Reprint Please specify the source Thank you: http://blog.csdn.net/vmurder/article/details/42963375
Test instructions
Each string in that input is first a length and then the string.
Then if the name of a cat ABCD Efgh, then call ABC,BCD,FG and so on is good, but the CDE is not.
And then enter a name with one line in the format
A a number, b b number.
A indicates the last name, and B is the name.
Exercises
Direct violence enumerates which substrings each name is,
Then we find that we can use the suffix array to optimize this thing ~ ~
Time complexity is inaccurate, which means that it can be stuck in a tle, but everyone has no positive solution,
So there should be no (PO) Heart (PO) disease (q) Mad (q) (q) to hack the problem.
So feel at ease and mess around.
Code:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 501000# Define M 50100#define inf 10100using namespace Std;int s[n],len;int sa[n],rank[n],height[n];int cnt[n],val[n],_val[n]; int stk[n],top;int n,m,crs[n];struct query{int n,s;} Query[m];int ans[m],vis[m];void Input () {int i,j,k,fengefu=inf;scanf ("%d%d", &n,&m);//To avoid multiple matches, the delimiter is different for ( i=1;i<=n;i++)//read into each meow name, with a separator in the middle. {for (scanf ("%d", &k), k--;) {crs[len]=i;scanf ("%d", &s[len++]);} S[len++]=++fengefu;for (scanf ("%d", &k), k--;) {crs[len]=i;scanf ("%d", &s[len++]);} S[len++]=++fengefu;} for (i=1;i<=m;i++)//Enter each name {scanf ("%d", &QUERY[I].N); Query[i].s=len;for (j=1;j<=query[i].n;j++) scanf ("%d", &s[len++]); s[len++]=++fengefu;}} inline BOOL cmp (int x,int y,int hl) {return val[x]==val[y]&& (X+hl>=len&&y+hl>=len) | | (X+hl<len&&y+hl<len&&val[x+hl]==val[y+hl]));} void SA (int lim=256) {int i,j,k,hl;for (i=0;i<lim;i++) cnt[i]=0;for (i=0;i<len;i++) cnt[val[i]=s[i]]++;for (i=1;i<lim;i++) cnt[i]+=cnt[i-1];for (i=len-1;i>=0;i--) sa[--cnt[val [I]] =i;for (k=0;; k++) {top=0,hl=1<<k;for (i=0;i<len;i++) if (Sa[i]+hl>=len) stk[++top]=sa[i];for (i=0;i<len;i++) if (sa[ I]>=HL) stk[++top]=sa[i]-hl;for (i=0;i<lim;i++) cnt[i]=0;for (i=0;i<len;i++) cnt[val[i]]++;for (i=1;i< lim;i++) cnt[i]+=cnt[i-1];for (i=len;i;i--) sa[--cnt[val[stk[i]]]]=stk[i];for (i=lim=0;i<len;lim++) {for (j=i;j <LEN-1&&CMP (SA[J],SA[J+1],HL); j + +); for (; i<=j;i++) _val[sa[i]]=lim;} for (i=0;i<len;i++) val[i]=_val[i];if (Lim==len) break;} for (i=0;i<len;i++) rank[sa[i]]=i;for (k=i=0;i<len;i++) {if (k) k--;if (!rank[i]) continue;while (s[i+k]==s[sa[ RANK[I]-1]+K]) k++;height[rank[i]]=k;}} void work () {int i,j,k;int l,r,res;for (i=1;i<=m;i++) {l=r=rank[query[i].s];while (HEIGHT[L]>=QUERY[I].N) l--; while (HEIGHT[R]>=QUERY[I].N) r++;r--, res=0;for (j=l;j<=r;j++) {if (Crs[sa[j]]) {if (vis[crs[sa[j]]]!=i) {vis[ crs[sa[j]]]=i;res++;ans[crs[sa[j]]]++;}}} Printf("%d\n", res);} for (i=1;i<=n;i++) {printf ("%d", ans[i]), if (i!=n) Putchar (");}} int main () {freopen ("test.in", "R", stdin); Input (); SA (200000); Work (); return 0;}
"BZOJ2754" "SCOI2012" the name suffix array on the Meow planet to optimize violence