zoukankan      html  css  js  c++  java
  • 经典算法回顾2

    public class Main1 {
        static String s1 = "abcd";
        static String s2 = "abce";
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            /*
             * 字符串相似度
             * 概念:
             * 对于两个字符串A和B,通过基本的增删改将字符串A改成B,或者将B改成A,
             * 在改变的过程中使用的最小步骤称之为“编辑距离”
             */
            System.out.println(LD());
        }
        public static int LD(){
            int a[][] = new int[s1.length()+1][s2.length()+1];
            for(int i = 0; i <= s1.length(); i++){
                a[i][0] = i;
            }
            for(int j = 0; j <= s2.length(); j++){
                a[0][j] = j;
            }
            for(int i = 1; i <= s1.length(); i++){
                for(int j = 1; j <= s2.length(); j++){
                    if(s1.getBytes()[i-1] == s2.getBytes()[j-1]){
                        a[i][j] = a[i-1][j-1];
                    }
                    else{
                        int temp = Math.min(a[i-1][j], a[i][j-1]);
                        a[i][j] = Math.min(temp, a[i-1][j-1]) + 1;
                    }
                }
            }
            return a[s1.length()][s2.length()];
        }
    }
    public class Main2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            /*
             * KMP算法
             */
            String str1 = "ababcabababdc";
            String str2 = "abc";
            System.out.println("fff");
            int index = KMP(str1,str2);
            System.out.println(index);
        }
        static int KMP(String str1, String str2){
            int i = 0;
            int j = 0;
            int[] next = new int[str2.length()];
            next = GetNextVal(str2);
            while(i < str1.length() && j < str2.length()){
                if(j == -1 || str1.getBytes()[i] == str2.getBytes()[j]){
                    i++;
                    j++;
                }
                else{
                    j = next[j];
                }
            }
            if(j == str2.length()){
                return i - str2.length();
            }
            return -1;
        }
        static int[] GetNextVal(String str2){
            int k= -1;
            int j = 0;
            int[] next = new int[str2.length()];
            next[j] = -1;
            while(j < str2.length() -1){
                if(k == -1 || str2.getBytes()[k] == str2.getBytes()[j]){
                    next[++j] = ++k;
                }
                else{
                    k = next[k];
                }
            }
            return next;
        }
    }
  • 相关阅读:
    python模块之__future__模块
    SQL之分组排序取top n
    SQL之层次查询
    win server 2008添加磁盘-脱机转换为联机状态方法
    拉链表-增量更新方法一
    一道hive SQL面试题
    一道与时间差有关的SQL面试题
    (转)linux中nmcli命令的使用及网络配置
    (转)内核模块操作命令-lsmod+rmmod+modinfo+modprobe
    (转)Linux 系统设置 : dmesg 命令详解
  • 原文地址:https://www.cnblogs.com/benniaoxuefei/p/4249102.html
Copyright © 2011-2022 走看看