-
给定两个字符串,求它们前后重叠的最长子串的长度,比如"abcde"和“cdefg”是"cde",长度为3。
-
输入可能包含多个测试案例。
对于每个测试案例只有一行, 包含两个字符串。字符串长度不超过1000000,仅包含字符'a'-'z'。 -
对应每个测试案例,输出它们前后重叠的最长子串的长度。
-
abcde cdefg
-
3
-
题目描述:
-
输入:
-
输出:
-
样例输入:
-
样例输出:
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string.h>
#include <stdio.h>
using namespace std;
int maxLen(const char* S, const char* T) {
int Slen = strlen(S), Tlen = strlen(T), i, j, minLen = min(Slen, Tlen), res = 0;
typedef unsigned long long ull;
ull Shash = 0, Thash = 0, t = 1, radix = 100000007;
for (int i = 1; i <= minLen; ++i) {
Shash = Shash + (S[Slen-i] - 'a') * t;
Thash = Thash * radix + (T[i-1] - 'a');
t *= radix;
if (Shash == Thash)
res = i;
}
return res;
}
char a[1000000], b[1000000];
int main() {
while (scanf("%s%s",a,b) != EOF) {
printf("%d\n",maxLen(a,b));
}
return 0;
}