Huawei OJ: Public String computing, Huawei oj
Note that this question is case-insensitive. Therefore, you must convert the input string to lowercase or uppercase before computation.
Second, the idea must be clear. First, match s1 WITH s2 [j] Starting from [I]. If s1 is not equal, j ++ continues until j is equal to s2.length () -1, equal, then I ++, j ++. Note: here is I ++, that is, the distance between the two I may exceed 1 when the matching starts from s [I] again next time. Set a counter in j.
import java.util.Scanner;public class findMaxSubStringLength {public static int getCommonStrLength(String s1,String s2){int count=0;if(s1==" "||s2==" ")return 0;for(int i=0;i<s1.length();i++){for(int j=0;j<s2.length();j++){int number=0;if(i==s1.length()||j==s2.length()){break;}while(s1.charAt(i)==s2.charAt(j)){i++;j++;number++;count=count>number?count:number;if(i==s1.length()||j==s2.length()){break;}if(count==s1.length()||count==s2.length())return count;}}}return count;}public static void main(String args[]){Scanner input=new Scanner(System.in);String s1=input.next();String s2=input.next();s1=s1.toLowerCase();s2=s2.toLowerCase();System.out.println(getCommonStrLength(s1,s2));}}
The problem of finding the maximum public substrings of two strings has been changed.
# Include <stdio. h>
# Include <malloc. h>
# Include <string. h>
# Include <stdlib. h>
# Define Max size 100
Typedef struct
{
Char str [MaxSize];
Int len;
} SeqString;
Int SubSeqString (SeqString * Sub, SeqString S, int pos, int len)
{
Int I;
If (pos <0 | pos> S. len | len> S. len-pos | len <1)
{
Sub-> len = 0;
Return 0;
}
Else
{
For (I = 0; I <len; I ++)
{
Sub-> str [I] = S. str [I + pos];
}
Sub-> str [I] = '\ 0'; // string end
Sub-> len = len;
Return 1;
}
}
Int MaxSameSeqString (SeqString S, SeqString T, SeqString * M)
{
Int I, j, k, t, pos;
Int len = 0, maxlen = 0;
For (I = 0; I <S. len; I ++)
{
K = I;
For (j = 0; j <T. len; j ++)
{
T = j;
Len = 0; // len value 0
If (S. str [I] = T. str [j])
{
While (S. str [k ++] = T. str [t ++])
Len ++;
If (maxlen <len)
{
Maxlen = len;
Pos = I;
}
}
}
}
If (maxlen> = 1)
{
SubSeqString (M, S, pos, maxlen); // The maxlenlen parameter.
Return 1;
}
Return 0;
}
Int main ()
{
SeqString X, Y;
SeqString * R = (SeqString *) malloc (sizeof (SeqString ));
Char a [MaxSize], B [MaxSize];
Scanf ("% s", );
Scanf ("% s", B );
Int alen, blen;
Alen = strlen ();
Blen = strlen (B );
X. len = alen;
Y. len = blen;
Strcpy (X. str, );
Strcpy (Y. str, B );
MaxSameSeqString (X, Y, R );
Printf ("% s", R-> str );
Return 0 ;}
How to find the maximum public substring of two strings? Baidu big public substrings
I want to talk about how many people come to copy Dynamic Planning Algorithms.