java查找两个字符串的最长公共子串
package test;
import java. util. Vector;
public class test {
public static String getChildStr ( String str1, String str2)
{
int [ ] [ ] record= new int [ str1. length ( ) ] [ str2. length ( ) ] ;
int maxLen = 0 , maxEnd = 0 ;
for ( int i = 0 ; i < str1. length ( ) ; ++ i)
for ( int j = 0 ; j < str2. length ( ) ; ++ j)
{
if ( str1. charAt ( i) == str2. charAt ( j) )
{
if ( i == 0 || j == 0 )
{
record[ i] [ j] = 1 ;
}
else
{
record[ i] [ j] = record[ i - 1 ] [ j - 1 ] + 1 ;
}
}
else
{
record[ i] [ j] = 0 ;
}
if ( record[ i] [ j] > maxLen)
{
maxLen = record[ i] [ j] ;
maxEnd = j;
}
}
return str2. substring ( maxEnd - maxLen + 1 , maxLen) ;
}
public static void main ( String[ ] args) {
long start= System. currentTimeMillis ( ) ;
System. out. println ( getChildStr ( "ajhf;ajkdjan;kjdfck;fjdfnkjdasjfo;iaejfaeiojrdfoijoifdjvo;iaejroifjfvaoedrjvodifjv;ILOVEYOU" , "ILOVEyou" ) ) ;
long end= System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
}