·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2754
根据黄学长的代码打的,对于每一个结点开一个map,非常暴力但是确实可以过
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int n,m;
map<int,int> ch[100005];
vector<int> a[20005],st[100005],V,M;
bool vis[100005],mark[50005];
int cnt=1,fail[100005];
int ans1[100005],ans2[100005];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void ins(int id) {
int L=read(),x,p=1;
for (int i=1;i<=L;i++) {
x=read();
if (!ch[p][x]) ch[p][x]=++cnt;
p=ch[p][x];
}
st[p].push_back(id);
}
void getfail () {
queue<int> q;
q.push(1);
while (!q.empty()) {
int p=q.front();q.pop();
for (map<int,int>::iterator i=ch[p].begin();i!=ch[p].end();i++) {
int c=i->first,k=fail[p];
while (!ch[k][c]) k=fail[k];
fail[i->second]=ch[k][c];
q.push(i->second);
}
}
}
void get (int id,int x) {
for (int i=x;i;i=fail[i])
if (!vis[i]) {
vis[i]=1;V.push_back(i);
for (int j=0;j<st[i].size();j++)
if (!mark[st[i][j]]) {
mark[st[i][j]]=1;M.push_back(st[i][j]);
ans1[st[i][j]]++;
ans2[id]++;
}
}
else break;
}
void solve (int x) {
int p=1;
for (int i=0;i<a[x].size();i++) {
int c=a[x][i];
while (!ch[p][c]) p=fail[p];
p=ch[p][c]; get(x,p);
}
for (int i=0;i<V.size();i++) vis[V[i]]=0;
for (int i=0;i<M.size();i++) mark[M[i]]=0;
V.clear();M.clear();
}
int main () {
for (int i=-1;i<=10000;i++)
ch[0][i]=1;
fail[1]=0;
n=read(); m=read();
int L,x;
for (int i=1;i<=n;i++) {
L=read();
while (L--) x=read(),a[i].push_back(x);
a[i].push_back(-1);
L=read();
while (L--) x=read(),a[i].push_back(x);
}
for (int i=1;i<=m;i++)
ins(i);
getfail();
for (int i=1;i<=n;i++)
solve(i);
for (int i=1;i<=m;i++) printf("%d\n",ans1[i]);
for (int i=1;i<=n;i++) {
printf("%d",ans2[i]);
if (i!=n) printf(" ");
}
return 0;
}