"Topic link" click
"The main topic"
gives some strings asking for the longest common prefix of any two strings queried
Exercises
string concatenation, the concatenation of the string to do a suffix array, for the query of two strings,
Just query the interval minimum on the height array.
Special attention to multiple sets of data when the end of the string processing, long time did not write easily ignored led WA.
Code
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int N=4000010;int N , M,rank[n],sa[n],h[n],tmp[n],cnt[n],ans;char t[n],s[n];void suffixarray (int n,int m) {int i,j,k;n++; for (i=0;i<2*n+5;i++) rank[i]=sa[i]=h[i]=tmp[i]=0; for (i=0;i<m;i++) cnt[i]=0; for (i=0;i<n;i++) cnt[rank[i]=s[i]]++; for (i=1;i<m;i++) cnt[i]+=cnt[i-1]; for (i=0;i<n;i++) sa[--cnt[rank[i]]]=i; for (K=1;k<=n;k<<=1) {for (i=0;i<n;i++) {j=sa[i]-k; if (j<0) j+=n; Tmp[cnt[rank[j]]++]=j; }sa[tmp[cnt[0]=0]]=j=0; for (i=1;i<n;i++) {if (rank[tmp[i]]!=rank[tmp[i-1]]| | RANK[TMP[I]+K]!=RANK[TMP[I-1]+K]) cnt[++j]=i; Sa[tmp[i]]=j; }memcpy (rank,sa,n*sizeof (int)); memcpy (sa,tmp,n*sizeof (int)); if (j>=n-1) break; }for (j=rank[h[i=k=0]=0];i<n-1;i++,k++) while (~k&&s[i]!=s[sa[j-1]+k]) h[j]=k--, j=rank[sa[j]+1];} int f[n][30],lg2[n];void Rmq_init(int n) {for (int i=2;i<=n;i++) lg2[i]=lg2[i/2]+1; for (int i=1;i<=n;i++) f[i][0]=h[i]; for (int j=1, (1<<j) <=n;j++) for (int i=1;i+ (1<<J) -1<=n;i++) f[i][j]=min (f[i][j-1],f[i+ (1<< ;(j-1))][j-1]);} int rmq_min (int l,int r) {if (l>r) swap (l,r); l++; int k=lg2[r-l+1]; return min (f[l][k],f[r-(1<<k) +1][k]);} int T,pos[n],len[n];int Main () {scanf ("%d", &t); int Cas=1; while (t--) {printf ("Case%d:\n", cas++); scanf ("%d", &n); int u=1; for (int i=1;i<=n;i++) {scanf ("%s", &t); Len[i]=strlen (t); Pos[i]=u; for (int j=0;j<len[i];j++) S[U++]=T[J]; }s[u]=0; Pay particular attention to multiple sets of data s[u]=0!!!! Suffixarray (u,256); Rmq_init (U); int q; scanf ("%d", &q); while (q--) {int x, y; scanf ("%d%d", &x,&y); int ans=min (len[x],len[y]); if (x==y) printf ("%d\n", len[x]); else printf ("%d\n", Min (ans,rmq_miN (rank[pos[x]],rank[pos[y]))); }}return 0;}
UVA 12338:anti-rhyme Pairs (suffix array +st table)