找出两个字符串的最大公共子串,比如说s1="asdeferdere",s2="cvvsdefrrt"两者的最大公共子串为"sdef"

解决方案 »

  1.   


    public static void main(String args[]){
    String str1 = "asdeferdere";
    String str2 = "cvvsdefrrt";
    String temp = str1.length()>str2.length()?str2:str1;
    String temp2 = temp==str1?str2:str1;
    int maxCount = 0;
    String maxSequence = "";
    for(int i=0;i<temp.length();i++){
    for(int j=i+1;j<temp.length();j++){
    if(temp2.contains(temp.substring(i, j))){
    int tempCount = j - i;
    if(tempCount>maxCount){
    maxCount = tempCount;
    maxSequence = temp.substring(i, j);
    }
    }
    }
    }
    System.out.println(maxSequence);
    }
      

  2.   

    我这个好长package xy;import java.util.LinkedList;
    import java.util.List;public class Clothes {
    public static void main(String[] args) {
    String s2 = "45aefg1234";
    String s1 = "12345aesadaefgfewfw";
    zdggzc(s2, s1);
    } static void zdggzc(String s1, String s2) {
    String temp;
    if (s1.length() < s2.length()) {
    temp = s1;
    s1 = s2;
    s2 = temp;
    temp = null;
    }
    List<String> zdggzc = new LinkedList<String>();
    int result = 0;
    StringBuilder maxSubStr = new StringBuilder(); for (int i = 0; i <= s1.length() - 1; i++) {
    char curChar = s1.charAt(i);
    int indexInS2 = 0;
    boolean flag = false;
    while (true) {
    if (flag) {
    break;
    }
    if (indexInS2 > s2.length() - 1) {
    break;
    }
    indexInS2 = s2.indexOf(curChar, indexInS2);
    if (indexInS2 == -1) {
    break;
    }
    maxSubStr.append(curChar);
    int offInS1 = i;
    int offInS2 = indexInS2;
    while (true) {
    offInS1++;
    offInS2++;
    if (offInS1 > s1.length() - 1 || offInS2 > s2.length() - 1) {
    flag = true;
    break;
    }
    if (s1.charAt(offInS1) == s2.charAt(offInS2)) {
    maxSubStr.append(s1.charAt(offInS1));
    } else {
    indexInS2++;
    break;
    }
    } if (maxSubStr.length() >= result) {
    result = maxSubStr.length();
    zdggzc.add(maxSubStr.toString());
    }
    maxSubStr.delete(0, maxSubStr.length());
    }
    }
    for (String string : zdggzc) {
    System.out.println(string);
    }
    }
    }
      

  3.   

    改了一下一楼的的循环方式:
    import java.util.Scanner;public class For_Test {
    public static void main(String args[]){ 
    Scanner scan=new Scanner(System.in);
    System.out.println("Input your string1:");
    String str1 = scan.next();
    System.out.println("Input your string2:");
    String str2 = scan.next(); 
    String temp = str1.length()>str2.length()?str2:str1; 
    String temp2 = temp==str1?str2:str1; 
    int maxCount = 0; 
    String maxSequence = ""; 
    for(int i=0;i<=temp.length()-1;i++){ 
    for(int j=i;j<=temp.length()-1;j++){ 
    if(temp2.contains(temp.substring(i, j+1))){ 
    int tempCount = j - i+1;
                            if(tempCount>maxCount){ 
                             maxCount = tempCount; 
                             maxSequence = temp.substring(i, j+1); 
                             } 
                            } 


    System.out.println("The max Sequence is:"+maxSequence); 

    }
      

  4.   

    我这样写行吗
    public class String3 {
    public static void main(String[] args) {
    // TODO Auto-generated method stub String s1="aefrereff";
    String s2="vbfereffmm";
    int m=s1.length();
    int n=s2.length();
    String s4=null;
    for(int i=0;i<m-1;i++)
    {
    for(int j=0;j<m-i;j++)
    {
    String s3=s1.substring(j,j+i+1);
    if(s2.indexOf(s3)!=(-1))
    {
     s4=s3;
    }
    }

    }
    System.out.println(s4);
    }}
      

  5.   


    public class Test {
    public static void main(String[] args) {
    String s1 = "asdeferdere",s2="cvvsdefrrt";
    String smallStr,largeStr;
    if(s1.length() > s2.length()){
    smallStr = s2;
    largeStr = s1;
    }else {
    smallStr = s1;
    largeStr = s2;
    }
    String str;
    int max = 0,count = 0;
    String resultStr = null;
    for(int i = 0;i < smallStr.length();i++){
    str = String.valueOf(smallStr.charAt(i));
    count = 0;
    for(int j = i + 1;j < smallStr.length();j++){
    if(largeStr.contains(str)){
    count++;
    if(count > max){
    max = count;
    resultStr = str;
    }
    str += String.valueOf(smallStr.charAt(j));
    }
    else {
    break;
    }
    }
    }
    System.out.println("same str : " + resultStr + " size : " + resultStr.length());
    }
    }
      

  6.   

    我推荐这种
    当字串大小等于m时,用
    用两个set把 等于m的字串都放进去出来,然后比较,用系统的方法,这样可以少很少重复操作,当没找到、
    m-1重复上面操作。直到找到位置return;
      

  7.   

    import java.util.ArrayList;
    import java.util.List;public class Test {

    public static void main(String[] args){

    String s1="asdeferdefr";
    String s2="cvvsdefrrt";

    System.out.println(method(s1,s2));
    }

    /**
     * 求s1和s2的最大公共字符串
     * @param s1
     * @param s2
     * @return
     */
    public static List method(String s1,String s2){
    List list =null;
    boolean isHasStr=false;
    for(int i=1;i<=s1.length();i++){
    isHasStr=false;
    for(int j=0;j<=s1.length()-i;j++){
    String temp=s1.substring(j, j+i);
    if(isHasStr(s2,temp)){
    if(!isHasStr){
    isHasStr=true;
    list=new ArrayList();
    }
    list.add(temp);
    }
    }
    }
    return list;
    }
    /**
     * 第一个字符串中是否含有第二个字符串,有返回true,否则返回false
     * @param str 第一个字符串
     * @param sub 第二个字符串
     * @return  第一个字符串中含有第二个字符串返回true,否则返回false
     */
    public static boolean isHasStr(String str,String sub){
    if(str.indexOf(sub)>=0){
    return true;
    }
    return false;
    }}
      

  8.   

    我也来写一个public static void main(String args[]) throws Exception{

    String str1 = "asdeferdere";
    String str2 = "cvvsdefrrt";
    int m = str1.length();
    int n = str2.length();
    int maxLen = 0;
    int sIndex = -1; for(int i=0; (i+maxLen)<m ; i++){
    int tmp = 0;
    for(int j=0; j<n; j++){
    if((i+tmp)<m && str1.charAt(i+tmp) == str2.charAt(j)) 
    tmp++;
    else{
    if(tmp > maxLen){
    maxLen = tmp;
    sIndex = i;
    }
    tmp = 0;

    }
    }

    }

    System.out.println(str1.substring(sIndex, maxLen+sIndex));

    }
      

  9.   

    CharAt的效率,要比substring高很多