MLE了...囧,不过算法是正确的
#define maxn 2000010
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int r[maxn],sa[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b] && r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m){//n要加1
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) wss[i]=0;
for(i=0;i<n;i++) wss[x[i]=r[i]]++;
for(i=1;i<m;i++) wss[i]+=wss[i-1];
for(i=n-1;i>=0;i--) sa[--wss[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) wss[i]=0;
for(i=0;i<n;i++) wss[wv[i]]++;
for(i=1;i<m;i++) wss[i]+=wss[i-1];
for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
int rank[maxn],height[maxn];//rank[i]:i排第几;sa[i]:排第i的后缀串在哪里,互为逆运算
void calheight(int *r,int *sa,int n){//n不用加1
int i,j,k=0;
for(i=1;i<=n;i++) rank[sa[i]]=i;
for(i=0;i<n;height[rank[i++]]=k){
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}
return;
}
int dp[20][maxn];
void init_rmq(int n){
int i,j;
for(i=1;i<=n;i++){
dp[0][i] = height[i];
}
int t = floor(log((double)n)/log(2.0));//向下取整
for(i=1;i<=t;i++){
for(j=1;j+(1<<(i-1))<=n;j++){
dp[i][j] = min(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
}
}
}
int lcp(int i,int j){//i开始的后缀,j开始的后缀,返回二者lcp
i = rank[i],j = rank[j];
if(i>j)swap(i,j);
i++;
int k = floor(log((j-i+1)*1.0)/log(2.0));
return min(dp[k][i],dp[k][j-(1<<k)+1]);
}
char str[maxn/2];
int main(){
int t=1;
while(scanf("%s",str)){
if(strcmp(str,"END") == 0)break;
int i,j;
int len = strlen(str);
int n = 0;
for(i=0;i<len;i++){
r[n++] = str[i];
}
r[n++] = '#';
for(i=len-1;i>=0;i--){
r[n++] = str[i];
}
r[n] = 0;
da(r,sa,n+1,199);
calheight(r,sa,n);
init_rmq(n);
int ans=0;
for(i=0;i<len;i++){
int tmp = lcp(i,n-i-1);//处理奇数
ans = max(ans,2*tmp-1);
tmp = lcp(i,n-i);//处理偶数
ans = max(ans,2*tmp);
}
printf("Case %d: %d\n",t++,ans);
}
return 0;
}
后缀数组虽然强大,但是缺点就是常数大,空间大,就像这题。。。改用扩展kmp就明显不同,空间、时间都有明显改进,思路参考这里http://greatkongxin.blog.163.com/blog/static/17009712520117285717159/ 复杂度为O(nlgn)
#define N 1100100
int b[N];
void ex_kmp(char *s,char *t,int ls,int lt,int *a){
int i,j,k;
j = 0;
while(j+1<lt && t[j]==t[j+1])j++;
b[0] = lt,b[1] = j,k = 1;
for(i=2;i<lt;i++){
int Len = k+b[k],L = b[i-k];
if(i+L < Len){
b[i] = L;
} else {
j = max(0,Len-i);
while(i+j<lt && t[i+j]==t[j]){
j++;
}
b[i] = j,k = i;
}
}
j = 0;
while(s[j]==t[j])j++;
a[0] = j,k = 0;
for(i=1;i<ls;i++){
int Len = k+a[k],L = b[i-k];
if(i+L < Len){
a[i] = L;
} else {
j = max(0,Len-i);
while(i+j<ls && s[i+j]==t[j]){
j++;
}
a[i] = j,k = i;
}
}
}
char s[N],t[N]; //s是主串,t是匹配串,下标均从1开始
int a1[N], a2[N];
char str[N];
int ans;
void dfs(char *str,int n){
int i,j;
if(ans>=n || n<2)return;
int mid = (n>>1);
for(i=mid;i<n;i++){
t[i-mid] = str[i];
}t[i-mid] = '\0';
for(i=n-1;i>=0;i--){
s[n-i-1] = str[i];
}s[n-i-1] = '\0';
ex_kmp(s,t,n,n-mid,a1);
///
for(i=0;i<mid;i++){
t[i] = str[mid-i-1];
}t[i] = '\0';
for(i=0;i<n;i++){
s[i] = str[i];
}s[i] = '\0';//
ex_kmp(s,t,n,mid,a2);
a1[n] = a2[n] = 0;
for(i=0;i<mid;i++){
if(a2[i]>=mid-i){
int x = mid-i+2*a1[n-i];
ans = max(ans,x);
}
}
for(i=mid;i<n;i++){
if(a1[n-i]>=i-mid){
int x = i-mid+2*a2[i];
ans = max(ans,x);
}
}
dfs(str,mid);
dfs(str+mid,n-mid);
}
int main(){FRE;
int ca = 1;
while(scanf("%s",str) && str[0]!='E'){
int len = strlen(str);
ans = 1;
dfs(str,len);
printf("Case %d: %d\n",ca++,ans);
}
return 0;
}