bzoj2754:[SCOI2012]喵星球上的点名(后缀自动机)

Description

a180285幸运地被选做了地球到喵星球的留学生。他发现喵星人在上课前的点名现象非常有趣。   假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成。喵星球上的老师会选择M个串来点名,每次读出一个串的时候,如果这个串是一个喵星人的姓或名的子串,那么这个喵星人就必须答到。 然而,由于喵星人的字码过于古怪,以至于不能用ASCII码来表示。为了方便描述,a180285决定用数串来表示喵星人的名字。
现在你能帮助a180285统计每次点名的时候有多少喵星人答到,以及M次点名结束后每个喵星人答到多少次吗?

Input

 
现在定义喵星球上的字符串给定方法:
先给出一个正整数L,表示字符串的长度,接下来L个整数表示字符串的每个字符。
输入的第一行是两个整数N和M。
接下来有N行,每行包含第i 个喵星人的姓和名两个串。姓和名都是标准的喵星球上的
字符串。
接下来有M行,每行包含一个喵星球上的字符串,表示老师点名的串。

Output

 
对于每个老师点名的串输出有多少个喵星人应该答到。
然后在最后一行输出每个喵星人被点到多少次。

Sample Input

2 3
6 8 25 0 24 14 8 6 18 0 10 20 24 0
7 14 17 8 7 0 17 0 5 8 25 0 24 0
4 8 25 0 24
4 7 0 17 0
4 17 0 8 25

Sample Output


2
1
0
1 2
「提示」
事实上样例给出的数据如果翻译成地球上的语言可以这样来看
2 3
izayoi sakuya
orihara izaya
izay
hara
raiz

HINT

「数据范围」
对于30%的数据,保证:
1<=N,M<=1000,喵星人的名字总长不超过4000,点名串的总长不超过2000。
对于100%的数据,保证:
1<=N<=20000,1<=M<=50000,喵星人的名字总长和点名串的总长分别不超过100000,保证喵星人的字符串中作为字符存在的数不超过10000。

题解

  这题的做法真的是莫名其妙的多

  什么后缀数组+主席树啦,AC自动机爆搞啦,fail树+树状数组啦

  然而蒟蒻连这题能用SAM都没发现……

  先把所有的名字和姓建一个广义SAM,然后在子串末尾记录一下,询问直接在SAM上跑一跑

  第二问怎么做呢?在每一个询问串匹配到的末尾记录一下,然后询问串上去乱跑

  大概就这样了吧……

 1 //minamoto
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<map>
 5 #include<iostream>
 6 using namespace std;
 7 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
 8 char buf[1<<21],*p1=buf,*p2=buf;
 9 inline int read(){
10     #define num ch-'0'
11     char ch;bool flag=0;int res;
12     while(!isdigit(ch=getc()))
13     (ch=='-')&&(flag=true);
14     for(res=num;isdigit(ch=getc());res=res*10+num);
15     (flag)&&(res=-res);
16     #undef num
17     return res;
18 }
19 char sr[1<<21],z[20];int C=-1,Z;
20 inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
21 inline void print(int x,int ch){
22     if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
23     while(z[++Z]=x%10+48,x/=10);
24     while(sr[++C]=z[Z],--Z);sr[++C]=ch;
25 }
26 const int N=5e4+5,M=2e5+5;
27 int a[N],b[N],ans[N],s[M],n,m;
28 int last,cnt=1,fa[M<<1],l[M<<1],sz[M<<1],las[M<<1],vis[M<<1];map<int,int> ch[M<<1];
29 void ins(int c){
30     int p=last,np=++cnt;last=np,l[np]=l[p]+1;
31     for(;p&&!ch[p].count(c);p=fa[p]) ch[p][c]=np;
32     if(!p) fa[np]=1;
33     else{
34         int q=ch[p][c];
35         if(l[q]==l[p]+1) fa[np]=q;
36         else{
37             int nq=++cnt;l[nq]=l[p]+1;
38             ch[nq]=ch[q];
39             fa[nq]=fa[q],fa[np]=fa[q]=nq;
40             for(;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
41         }
42     }
43 }
44 inline void update1(int x,int y){
45     for(;x&&las[x]!=y;x=fa[x])
46     ++sz[x],las[x]=y;
47 }
48 inline void update2(int x,int y){
49     for(;x&&las[x]!=y;x=fa[x])
50     ans[y]+=vis[x],las[x]=y;
51 }
52 int main(){
53     n=read(),m=read();
54     int tot=0;cnt=1;
55     for(int i=1;i<=n;++i){
56         a[i]=read();
57         last=1;
58         for(int j=1;j<=a[i];++j)
59         s[++tot]=read(),ins(s[tot]);
60         b[i]=read();
61         last=1;
62         for(int j=1;j<=b[i];++j)
63         s[++tot]=read(),ins(s[tot]);
64     }
65     tot=0;
66     for(int i=1;i<=n;++i){
67         for(int j=1,x=1;j<=a[i];++j) update1(x=ch[x][s[++tot]],i);
68         for(int j=1,x=1;j<=b[i];++j) update1(x=ch[x][s[++tot]],i);
69     }
70     while(m--){
71         int len=read();bool flag=1;int x=1;
72         for(int i=1;i<=len;++i){
73             int k=read();
74             if(flag)
75             ch[x].count(k)?x=ch[x][k]:flag=false;
76         }
77         flag?(++vis[x],print(sz[x],10)):(print(0,10));
78     }
79     tot=0;
80     for(int i=1;i<=cnt;++i) las[i]=0;
81     for(int i=1;i<=n;++i){
82         for(int j=1,x=1;j<=a[i];++j) update2(x=ch[x][s[++tot]],i);
83         for(int j=1,x=1;j<=b[i];++j) update2(x=ch[x][s[++tot]],i);
84     }
85     for(int i=1;i<=n;++i) print(ans[i],32);
86     Ot();
87     return 0;
88 }

 

posted @ 2018-08-13 11:33  bztMinamoto  阅读(424)  评论(0编辑  收藏  举报
Live2D