【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

【BZOJ3942】[Usaco2015 Feb]Censoring

Description

Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).

FJ has taken all of the text from the magazine to create the string S of length at most 10^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the _first_ occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

Please help FJ determine the final contents of S after censoring is complete

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

Input

The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

Sample Input

whatthemomooofun
moo

Sample Output

whatthefun
题解:本题可以用KMP来做,不过hash也可以,方法是用栈来储存字符,一旦发现栈顶的hash值等于T串,就弹栈。
#include<stdio.h>
#include<string.h>
unsigned long long hs[1000010],ht;
unsigned long long seed[1000010];
int n,m;
int top;
char S[1000010],stack[1000010],T[1000010];
void BKDR()
{
    seed[1]=131;
    ht=T[0];
    int i;
    for(i=1;i<m;i++)
    {
        ht=ht*seed[1]+T[i];
        seed[i+1]=seed[i]*seed[1];
    }
}
int main()
{
    scanf("%s%s",S,T);
    n=strlen(S);
    m=strlen(T);
    BKDR();
    int i,j;
    for(i=0;i<m;i++)
    {
        stack[top++]=S[i];
        hs[top]=hs[top-1]*seed[1]+S[i];
    }
    for(i=m;i<n;i++)
    {
        while(top>=m&&hs[top]-hs[top-m]*seed[m]==ht)
            for(j=0;j<m;j++)
                stack[--top]='\0';
        stack[top++]=S[i];
        hs[top]=hs[top-1]*seed[1]+S[i];
    }
    while(top>=m&&hs[top]-hs[top-m]*seed[m]==ht)
        for(j=0;j<m;j++)
            stack[--top]='\0';
    printf("%s",stack);
    return 0;
}

【BZOJ3940】[Usaco2015 Feb]Censoring

题意:本题和上题一样,只是有多个T串,那么就不能用KMP和hash来做,要用AC自动机。

注意最后任意两个单词都没有包含关系,所以处理的时候不需要沿着fail树一直找,否则会TLE。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=100010;
struct node
{
    int fail,ch[26],cnt;
}p[maxn];
char str[maxn],w[maxn],ans[maxn];
int n,tot,len,sum;
int pos[maxn],q[maxn],l,r;
void build()
{
    int i,u,t;
    l=1;
    q[++r]=1;
    while(l<=r)
    {
        u=q[l++];
        for(i=0;i<26;i++)
        {
            if(!p[u].ch[i])
            {
                if(u==1)    p[u].ch[i]=1;
                else    p[u].ch[i]=p[p[u].fail].ch[i];
                continue;
            }
            q[++r]=p[u].ch[i];
            if(u==1)
            {
                p[p[u].ch[i]].fail=1;
                continue;
            }
            t=p[u].fail;
            while(!p[t].ch[i]&&t)    t=p[t].fail;
            if(t)    p[p[u].ch[i]].fail=p[t].ch[i];
            else    p[p[u].ch[i]].fail=1;
        }
    }
}
void search()
{
    int i,j,u,t;
    u=1;
    pos[0]=1;
    for(i=0;i<len;i++)
    {
        ans[++sum]=str[i];
        pos[sum]=p[pos[sum-1]].ch[str[i]-'a'];
        if(p[pos[sum]].cnt)    sum-=p[pos[sum]].cnt;
    }
    for(i=1;i<=sum;i++)    putchar(ans[i]);
}
int main()
{
    scanf("%s",str);
    scanf("%d",&n);
    len=strlen(str);
    int i,j,k,u;
    tot=1;
    for(i=1;i<=n;i++)
    {
        scanf("%s",w);
        k=strlen(w);
        u=1;
        for(j=0;j<k;j++)
        {
            if(!p[u].ch[w[j]-'a'])    p[u].ch[w[j]-'a']=++tot;
            u=p[u].ch[w[j]-'a'];
        }
        p[u].cnt=k;
    }
    build();
    search();
}
posted @ 2017-01-06 16:17 CQzhangyu 阅读(...) 评论(...) 编辑 收藏