HDU-2594 Simpsons’ Hidden Talents
题目链接:HDU-2594
题目大意:给定两个字符串 问第一个字符串前缀与第二个字符串的后缀的最大的重复部分有多长 不为0的话将他们输出
解题思路:花里胡哨的过不去 直接将两个字符串拼接进行kmp的findNext就好,注意两个字符串要找到最短的长度,然后1截取前最短,2截取后最短,然后findNext,注意如果结果比两个字符串的最小长度大的话,结果就是最小长度,不然aaa aa这种情况会出错
代码块:
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
string strA, strB;
int minX = 0;
int nex[100005];
int findNex() {
int lenA = strA.length();
int lenB = strB.length();
minX = lenA < lenB ? lenA : lenB;
string strC = "";
strC += strA.substr(0, minX);
strC += strB.substr(lenB - minX, lenB);
int i = 1, j = 0;
while(i < strC.length()){
if(strC[i] == strC[j]){
nex[i++] = ++j;
}else if(j != 0){
j = nex[j - 1];
}else{
nex[i++] = 0;
}
}
return nex[strC.length() - 1];
}
int main() {
while(cin>>strA>>strB) {
int res = findNex();
if(res){
if(res > minX) res = minX;
for(int i = 0; i < res; i++){
cout<<strA[i];
}
cout<<" ";
}
cout<<res<<endl;
memset(nex, 0, sizeof(nex));
}
return 0;
}