SA入门题,将2个串中间用另外的字符链接即可

调了半天一直以为是模板的错,原来是乘法超了intQAQ

不好意思。。传错代码,误认子弟(啪啪啪

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(int i=l;i<=r;i++)
 3 #define dec(i,l,r) for(int i=l;i>=r;i--)
 4 #define link(x) for(edge *j=h[x];j;j=j->next)
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define inf 1e9
 7 #define ll long long
 8 #define succ(x) (1<<x)
 9 #define lowbit(x) (x&(-x))
10 #define NM 200000+5
11 using namespace std;
12 int read(){
13     int x=0,f=1;char ch=getchar();
14     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
15     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
16     return x*f;
17 }
18 char st[NM];
19 int n,sa[NM],rank[NM],tmp[NM],top[NM],h[NM],ans,_t;
20 void getsa(){
21     int m=256;int j;
22     inc(i,0,n)top[rank[i]=(int)st[i]]++;
23     inc(i,1,m)top[i]+=top[i-1];
24     inc(i,0,n)sa[--top[rank[i]]]=i;
25     for(int k=1;k<=n;k<<=1){
26         inc(i,0,n){
27             j=sa[i]-k;
28             if(j<0)j+=n+1;
29             tmp[top[rank[j]]++]=j;
30         }
31         sa[tmp[top[0]=0]]=j=0;
32         inc(i,1,n){
33             if(rank[tmp[i]]!=rank[tmp[i-1]]||rank[tmp[i]+k]!=rank[tmp[i-1]+k])
34             top[++j]=i;
35             sa[tmp[i]]=j;
36         }
37         memcpy(rank,sa,sizeof sa);memcpy(sa,tmp,sizeof tmp);
38     }
39     j=0;
40     inc(i,0,n)if(rank[i]){
41         if(j)j--;
42         while(st[i+j]==st[sa[rank[i]-1]+j])j++;
43         h[rank[i]]=j;
44     }
45 }
46 int main(){
47     freopen("data.in","r",stdin);
48     scanf("%s",st);
49     _t=n=strlen(st);st[n]='$';
50     scanf("%s",st+n+1);
51     n=strlen(st);st[n]='$';
52     getsa();
53     inc(i,1,n)
54     if((ll)(sa[i]-_t)*(sa[i-1]-_t)<0)ans=max(ans,h[i]);
55     printf("%d\n",ans);
56     return 0;
57 }
View Code

 

posted on 2016-02-10 18:17  onlyRP  阅读(268)  评论(0编辑  收藏  举报