SA入门题,将2个串中间用另外的字符链接即可
调了半天一直以为是模板的错,原来是乘法超了intQAQ
1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<queue> 7 #define inc(i,l,r) for(int i=l;i<=r;i++) 8 #define dec(i,l,r) for(int i=l;i>=r;i--) 9 #define link(x) for(edge *j=h[x];j;j=j->next) 10 #define mem(a) memset(a,0,sizeof(a)) 11 #define inf 1e9 12 #define ll long long 13 #define succ(x) (1<<x) 14 #define NM 200000+5 15 using namespace std; 16 int read(){ 17 int x=0,f=1;char ch=getchar(); 18 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 19 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 20 return x*f; 21 } 22 char s[NM]; 23 int n,_t,m,rank[NM],top[NM],sa[NM],tmp[NM],h[NM],ans; 24 void getsa(){ 25 m=256;int j=0; 26 inc(i,0,n)top[rank[i]=(int)s[i]]++; 27 inc(i,1,m)top[i]+=top[i-1]; 28 inc(i,0,n)sa[--top[rank[i]]]=i; 29 for(int k=1;k<=n;k<<=1){ 30 inc(i,0,n){ 31 j=sa[i]-k; 32 if(j<0)j+=n+1; 33 tmp[top[rank[j]]++]=j; 34 } 35 sa[tmp[top[0]=0]]=j=0; 36 inc(i,1,n){ 37 if(rank[tmp[i]]!=rank[tmp[i-1]]||rank[tmp[i]+k]!=rank[tmp[i-1]+k]) 38 top[++j]=i; 39 sa[tmp[i]]=j; 40 } 41 memcpy(rank,sa,sizeof(sa)); 42 memcpy(sa,tmp,sizeof(tmp)); 43 if(j>=n)break; 44 } 45 j=0; 46 inc(i,0,n){ 47 if(j)j--; 48 while(s[i+j]==s[sa[rank[i]-1]+j])j++; 49 h[rank[i]]=j; 50 } 51 } 52 int main(){ 53 // freopen("data.in","r",stdin); 54 scanf("%s",s); 55 _t=n=strlen(s); 56 s[n]=‘$‘; 57 scanf("%s",s+n+1); 58 n=strlen(s);s[n+1]=‘$‘; 59 getsa(); 60 inc(i,1,n) 61 if((ll)(sa[i]-_t)*(sa[i-1]-_t)<0)ans=max(ans,h[i]); 62 printf("%d\n",ans); 63 return 0; 64 }
时间: 2024-10-24 17:04:10