2754: [SCOI2012]喵星球上的点名 AC自动机+map

AC自动机上暴力跑匹配就好,字符集比较大用map。

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<cstring>
#define N 100005
using namespace std;
int n,m,cnt;
int ans1[N],ans2[N];
bool vis[N],mark[N>>1];
int p[N],q[N];
vector<int> a[N>>2],s[N],V,M;
map<int,int> mp[N];
inline int read()
{
    int a=0,f=1; char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
    while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
    return a*f;
}
inline void insert(int id)
{
    int L=read(),x;
    int now=1;
    while (L--) 
    {
        x=read();
        if (!mp[now][x]) mp[now][x]=++cnt;
        now=mp[now][x];
    }
    s[now].push_back(id);
}
inline void build_fail()
{
    int t=0,w=1,x;
    q[1]=1;
    while (t<w)
    {
        x=q[++t];
        map<int,int>::iterator it;
        for (it=mp[x].begin();it!=mp[x].end();it++)
        {
            int t=it->first,k=p[x];
            while (!mp[k][t]) k=p[k];
            p[it->second]=mp[k][t];
            q[++w]=it->second;
        }
    }
}
inline void get(int id,int x)
{
    for (int i=x;i;i=p[x])
        if (!vis[i])
        {
            vis[i]=1; V.push_back(i);
            for (int j=0;j<s[i].size();j++)
                if (!mark[s[i][j]])
                {
                    mark[s[i][j]]=1;
                    M.push_back(s[i][j]);
                    ans1[s[i][j]]++;
                    ans2[id]++;
                }
        }
        else break;
}
inline void solve(int x)
{
    int now=1;
    for (int i=0;i<a[x].size();i++)
    {
        int t=a[x][i];
        while (!mp[now][t]) now=p[now];
        now=mp[now][t]; get(x,now);
    }
    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()
{
    n=read(); m=read();
    for (int i=1;i<=n;i++)
    {
        int L=read(),x;
        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<=10000;i++) mp[0][i]=1; cnt=1;
    for (int i=1;i<=m;i++) insert(i);
    build_fail();
    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%c",ans2[i],i!=n?' ':'\n');
    return 0;
}

你可能感兴趣的:(2754: [SCOI2012]喵星球上的点名 AC自动机+map)