【Java】最长公共子串


 Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String name1 = in.nextLine();
            String name2 = in.nextLine();

            int sum = calcSimilarity(name1, name2);
            System.out.println(sum);
        }
        

    public static String getMaxLengthCommonString(String s1, String s2) {
        if (s1 == null || s2 == null)  {
            return null;
        }
        a = s1.length();//s1长度做行
        b = s2.length();//s2长度做列
        if(a== 0 || b == 0) {
            return "";
        }
        //设置匹配矩阵
        boolean [][] array = new boolean[a][b];
        for (int i = 0; i  < a; i++) {
            char c1 = s1.charAt(i);
            for (int j = 0; j < b; j++) {
                char c2 = s2.charAt(j);
                if (c1 == c2) {
                    array[i][j] = true;
                }  else {
                    array[i][j] = false;
                }
            }
        }
        //求所有公因子字符串,保存信息为相对第二个字符串的起始位置和长度
        List<ChildString> childStrings = new ArrayList<ChildString>();
        for (int i = 0; i < a; i++) {
            getMaxSort(i, 0, array, childStrings);
        }
        for (int i = 1; i < b; i++) {
            getMaxSort(0, i, array, childStrings);
        }
        //排序
        sort(childStrings);
        if (childStrings.size() < 1) {
            return "";
        }
        //返回最大公因子字符串
        int max = childStrings.get(0).maxLength;
        StringBuffer sb = new StringBuffer();
        for (ChildString s: childStrings) {
            if (max != s.maxLength) {
                break;
            }
            sb.append(s2.substring(s.maxStart, s.maxStart + s.maxLength));
        }
        return sb.toString();
    }

    //排序,倒叙
    private static void sort(List<ChildString> list) {
        Collections.sort(list, new Comparator<ChildString>(){
            public int compare(ChildString o1, ChildString o2) {
                return o2.maxLength - o1.maxLength;
            }
        });
    }

    //求一条斜线上的公因子字符串
    private static void getMaxSort(int i, int j, boolean[][] array, List<ChildString> sortBean) {
        int length = 0;
        int start = j;
        for (; i < a && j < b; i++,j++) {
            if (array[i][j]) {
                length++;
            } else {
                sortBean.add(new ChildString(length, start));
                length = 0;
                start = j + 1;
            }
            if (i == a-1 || j == b-1) {
                sortBean.add(new ChildString(length, start));
            }
        }
    }

    //公因子类
    static class ChildString {
        int maxLength;
        int maxStart;

        ChildString(int maxLength, int maxStart){
            this.maxLength = maxLength;
            this.maxStart = maxStart;
        }
    }
  • 24
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞 新

请小哥喝杯茶~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值