题目:输入两个字符串,求两个字符串的最大公共字串长度,忽略大小写。
例如输入 abcdegfe sdegsdegfds,最大的公共长度4。
1、在不限制时间复杂度的情况下,可以采取如下的代码。进行两次for循环,在for循环的内部调用String.contain()方法。但String.contain()方法在Java内部还是需要进行单个字符的,嵌套了2个for循环。这样时间复杂度就是O(n4)。
<span style="font-size:14px;">import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String pLine=in.nextLine().trim();
in.close();
String[] pStrings=pLine.split(" ");
System.out.println(getCommonStringCount(pStrings[0],pStrings[1]));
}
private static int getCommonStringCount(String pFirst,
String pSecond) {
int pFLength=pFirst.length();
int pSLength=pSecond.length();
if(pFLength<pSLength){
String temp;
temp=pFirst;
pFirst=pSecond;
pSecond=temp;
}
pFirst=pFirst.toLowerCase();
pSecond=pSecond.toLowerCase();
int pCommonCount=0;
int pSecondLength=pSecond.length();
for(int i=0;i<pSecondLength;i++){
for(int j=pSecondLength;j>=i+1;j--){
if( pFirst.contains(pSecond.substring(i, j))&&j-i>pCommonCount){
pCommonCount=j-i;
}
}
}
return pCommonCount;
}
}
</span>
2、利用动态规划的方法,通过一个二维数组来存储单个字符是否相等,相等则为1,。否则,则为0。
例如,字符串A="ahhajakja";B="KHaJHHJ"。
2.1 二维数组int[][] pTable=new int[A.length][B.length];数组用0和1填充。
2.2 遍历二维数组,pTable[i+1][j+1]=(pTable[i+1][j+1]==1)?pTable[i][j]+1:0;
2.3 遍历二维数组,输出pTable[i][j]最大的。
<span style="font-size:14px;">import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner pScanner = new Scanner(System.in);
String pLine = pScanner.nextLine().trim();
pScanner.close();
String[] pArrayStrings = pLine.split(" ");
char[] pFirstChars = pArrayStrings[0].toCharArray();
char[] pSecondChars = pArrayStrings[1].toCharArray();
System.out.println((int) getMaxLength(pFirstChars, pSecondChars));
}
private static int getMaxLength(char[] pFirstChars, char[] pSecondChars) {
if(pFirstChars.length>pSecondChars.length){
char[] pTempChars;
pTempChars=pFirstChars;
pFirstChars=pSecondChars;
pSecondChars=pTempChars;
}
int pFirstLength = pFirstChars.length;
int pSecondLength = pSecondChars.length;
pFirstChars = String.valueOf(pFirstChars).toLowerCase().toCharArray();
pSecondChars = String.valueOf(pSecondChars).toLowerCase().toCharArray();
int[][] pTable = new int[pFirstLength][pSecondLength];
for (int pRowIndex = 0; pRowIndex < pFirstLength; pRowIndex++) {
char temp = pFirstChars[pRowIndex];
for (int pColumn = 0; pColumn < pSecondLength; pColumn++) {
pTable[pRowIndex][pColumn]=(temp==pSecondChars[pColumn])?1:0;
}
}
for(int pRowIndex=1;pRowIndex<pFirstLength;pRowIndex++){
for(int pColumnIndex=1;pColumnIndex<pSecondLength;pColumnIndex++){
pTable[pRowIndex][pColumnIndex]=(pTable[pRowIndex][pColumnIndex]==0)?0:pTable[pRowIndex-1][pColumnIndex-1]+pTable[pRowIndex][pColumnIndex];
}
}
int pMaxCommonLength=0;
for(int pTableIndex=0;pTableIndex<pFirstLength*pSecondLength;pTableIndex++){
pMaxCommonLength=(pTable[pTableIndex/pSecondLength][pTableIndex%pSecondLength]>pMaxCommonLength)?pTable[pTableIndex/pSecondLength][pTableIndex%pSecondLength]:pMaxCommonLength;
}
return pMaxCommonLength;
}
}
</span>