题意:
输入 s1、s2,问s1的后缀 以及s2的前缀中 相同的字符最多有多少个?是什么?
解析:
利用前缀后缀特性,把两个字符串接起来,按照前面那题的方法,来求前缀后缀子串,注意其终止条件的变化,这里前缀后缀子串的长度必须小于s1,s2长度的最小值。
my code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 50000;
char str[N*2], str2[N];
int jump[N*2];
void getNext() {
int len = strlen(str);
int j = 0, k = -1;
jump[0] = -1;
while(j < len) {
if(k == -1 || str[j] == str[k])
jump[++j] = ++k;
else k = jump[k];
}
}
int main() {
while(scanf("%s", str) != EOF) {
scanf("%s", str2);
int len = strlen(str), len2 = strlen(str2);
strcat(str, str2);
getNext();
int minl = min(len, len2), cur = len+len2;
while(cur > minl)
cur = jump[cur];
if(cur > 0) {
for(int i = 0; i < cur; i++)
putchar(str[i]);
printf(" %d\n", cur);
}else puts("0");
}
return 0;
}