(算法学习)暴力解决最大公共子串的长度及个数

求字符串1与字符串2的最大公共子串的长度及此长度最大公共子串的个数。
实例:
输入:
abcdefg
Eebcdfg
输出:
3 1
输入:abcdefg
      abcddefg
输出:4 2

思路:

1)字符串1的子串

2)字符串的子串

3)找两个子串的相同子串,即公共子串

4)再找最长公共子串的长度和个数

代码分享:

package StringDemo;
//z最大公共子串及其长度
//求字符串1与字符串2的最大公共子串的长度及此长度最大公共子串的个数。
//实例:
//输入:
//abcdefg
//Eebcdfg
//输出:
//3 1
//输入:abcdefg
//      abcddefg
//输出:4 2

import java.util.*;

public class StringDemo16 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
      /*
      String str;
      int len;
      if(str1.length()<=str2.length()){
          len=str1.length();
          str=str1;
      }else{
          len=str2.length();
          str=str2;
      }

       */
        //字符串1所有的子串
        ArrayList arrayList = new ArrayList<>();
        for (int i = 0; i < str1.length(); i++) {
            for (int j = i + 1; j < str1.length() + 1; j++) {
                if (j != str1.length()) {
                    arrayList.add(str1.substring(i, j));
                    continue;
                } else {
                    arrayList.add(str1.substring(i));
                    break;
                }
            }
        }
        for (String s : arrayList) {
            System.out.println(s);
        }
        //字符串2所有的子串
        ArrayList arrayList2 = new ArrayList<>();
        for (int i = 0; i < str2.length(); i++) {
            for (int j = i + 1; j < str2.length() + 1; j++) {
                if (j != str2.length()) {
                    arrayList2.add(str2.substring(i, j));
                    continue;
                } else {
                    arrayList2.add(str2.substring(i));
                    //System.out.println("break");
                    //System.out.println(str2.substring(i));
                    break;
                }
            }
        }
        for (String s : arrayList2) {
            System.out.println(s);
        }

        //找公共子串
        ArrayList arrayListCommon = new ArrayList<>();//存放公共子串
        if (arrayList.size() <= arrayList2.size()) {
            for (String s : arrayList) {
                if (arrayList2.contains(s)) {
                    arrayListCommon.add(s);
                }
            }
        } else {
            for (String s : arrayList2) {
                if (arrayList.contains(s)) {
                    arrayListCommon.add(s);
                }
            }
        }
        //找最大公共子串
        int max = 0;
        HashMap hashMap = new HashMap<>();
        for (String s : arrayListCommon) {
            if (s.length() >=max){
                max=s.length();
                if(hashMap.containsKey(max)){
                    int oldValue=hashMap.get(max);
                    hashMap.put(max,oldValue+1);
                }else{
                    hashMap.put(max,1);
                }
            }
        }

        //输出
        System.out.println(max+"  "+hashMap.get(max));




    }


}

你可能感兴趣的:(java,最长公共子串,string)