Input
Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
Output
A single line with a single integer number – what is the maximum length of the original text written by the little cat.
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output
27
Time—1125ms
Memory—3836k
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e5+7;
char P[N],T[N];
ull hashp[N],hasht[N],pm[N],p=13331;
ull getseg(int l,int r,ull Hash[]){
return Hash[r]-Hash[l-1]*(pm[r-l+1]);
}
int l1,l2;
vector<ull>q;
bool C(int x){
q.clear();
for(int l=1,r=l+x-1;r<=l1;++l,++r)q.push_back(getseg(l,r,hashp));
sort(q.begin(),q.end());
for(int l=1,r=l+x-1;r<=l2;++l,++r){
ull tmp=getseg(l,r,hasht);
if(*lower_bound(q.begin(),q.end(),tmp)==tmp)return 1;
}
return 0;
}
int main(){
pm[0]=1;
for(int i=1;i<N;++i)pm[i]=pm[i-1]*p;
gets(P+1),gets(T+1);
l1=strlen(P+1),l2=strlen(T+1);
for(int i=1;i<=l1;++i)hashp[i]=hashp[i-1]*p+P[i];
for(int i=1;i<=l2;++i)hasht[i]=hasht[i-1]*p+T[i];
int l=0,r=min(l1,l2);
while(l<r){
int mid=(l+r+1)>>1;
if(C(mid))l=mid;
else r=mid-1;
}
printf("%d\n",l);
return 0;
}