标签:des style blog http io color os ar for
Description
Input
Output
Sample Input
input | output |
---|---|
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA |
ArozaupalanalapuazorA |
#include<iostream> #include<string.h> #include<ctype.h> #include<cstdio> #include<algorithm> using namespace std; #define Max 100020 char s[Max]; int sa[Max], Rank[Max], height[Max]; int wa[Max], wb[Max], wd[Max]; void build_sa(int n, int m){ // 倍增算法 n为总长度,n=l+1, m为字符范围 int i,j,p,*xy, *x = wa, *y = wb; for(i = 0; i < m; i ++) wd[i] = 0; for(i = 0; i < n; i ++) wd[x[i]=s[i]] ++; for(i = 1; i < m; i ++) wd[i] += wd[i-1]; for(i = n-1; i >= 0; i --) sa[-- wd[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 < m; i ++) wd[i] = 0; for(i = 0; i < n; i ++) wd[x[y[i]]] ++; for(i = 1; i < m; i ++) wd[i] += wd[i-1]; for(i = n-1; i >= 0; i --) sa[-- wd[x[y[i]]]] = y[i]; xy=x;x=y;y=xy; p=1;x[sa[0]]=0; for(i=1;i<n;i++){ x[sa[i]] = (y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++; } } } void getHeight(int n){ // 求height数组。 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];s[i+k]==s[j+k];k++); } //sa[1~~l]为有效值 sa[i]=a则代表排在第 i 位的是第a个后缀。 a属于[0~~l-1] 字符串比较不论长度 //Rank[0~~l-1]是有效值 Rank[i]=b则代表第 i 个后缀排在第b位 b属于[1~~l] //height[2~~l]是有效值 height[i]=c 则代表排在第 i 位的后缀和排在第i-1的后缀的最长前缀长度是c int strl=0; int max1(int ans,int num,int i){ if(num>ans) ans=num,strl=i; return ans; } int main(){ int l,i,ans,T,temp,j; while(scanf("%s",s)!=EOF){ l=strlen(s); int oldl=l; s[l]=‘|‘; for(i=l+1,j=l-1;i<=l+l;i++,j--){ s[i]=s[j]; } s[i]=‘\0‘; l=2*l+1; build_sa(l+1,200); getHeight(l); strl=0; ans=1; for(i=2;i<=l;i++){ int a = sa[i-1], b = sa[i]; if(a > b) swap(a, b); if(a < oldl && b > oldl && a+height[i] == l-b) { if(height[i] > ans) ans = height[i], strl = a; else if(height[i] == ans && a < strl) strl = a; } } for(i=strl;i<strl+ans;i++) printf("%c",s[i]); cout<<endl; } return 0; }
/*
xatag,ata
*/
标签:des style blog http io color os ar for
原文地址:http://www.cnblogs.com/woshijishu3/p/4063673.html