【刷题】BZOJ 3172 [Tjoi2013]单词

Description

某人读论文,一篇论文是由许多单词组成。但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次。

Input

第一个一个整数N,表示有多少个单词,接下来N行每行一个单词。每个单词由小写字母组成,N<=200,单词长度不超过10^6

Output

输出N个整数,第i行的数字表示第i个单词在文章中出现了多少次。

Sample Input

3
a
aa
aaa

Sample Output

6
3
1

Solution

AC自动机模板题
把所有串插入AC自动机,然后把所有串全部连起来,相邻两个之间隔一个特殊字符,然后跑AC自动机的匹配就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=200+10,MAXS=1000000+10;
int n,ch[MAXS][30],fail[MAXS],last[MAXS],ed[MAXS],ans[MAXN],cnt;
char s[MAXS+MAXN],t[MAXS+MAXN];
std::queue<int> q;
std::vector<int> V[MAXS];
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
    for(register int i=0,lt=strlen(s);i<lt;++i)s[i]-='a'-1;
}
inline void insert(int rk)
{
    int x=0;
    for(register int i=0,lt=strlen(s);i<lt;++i)
        if(!ch[x][s[i]])x=ch[x][s[i]]=++cnt;
        else x=ch[x][s[i]];
    ed[x]=1;
    V[x].push_back(rk);
}
inline void getfail()
{
    for(register int i=1;i<=26;++i)
        if(ch[0][i])q.push(ch[0][i]);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(register int i=1,y,z;i<=26;++i)
            if(ch[x][i])
            {
                y=ch[x][i],z=fail[x];
                while(z&&!ch[z][i])z=fail[z];
                fail[y]=ch[z][i];
                last[y]=ed[fail[y]]?fail[y]:last[fail[y]];
                q.push(y);
            }
            else ch[x][i]=ch[fail[x]][i];
    }
}
inline void save(int x)
{
    if(x)
    {
        if(ed[x])for(register int i=0,lt=V[x].size();i<lt;++i)ans[V[x][i]]++;
        save(last[x]);
    }
}
inline void match()
{
    int x=0;
    for(register int i=0,lt=strlen(s);i<lt;++i)x=ch[x][s[i]],save(ed[x]||last[x]?x:0);
}
int main()
{
    read(n);
    for(register int i=1;i<=n;++i)
    {
        scanf("%s",s);
        strcat(t,"{");strcat(t,s);
        init();insert(i);
    }
    getfail();
    for(register int i=0,lt=strlen(t);i<lt;++i)s[i]=t[i];
    init();match();
    for(register int i=1;i<=n;++i)write(ans[i],'\n');
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hongyj/p/9301960.html