【BZOJ3942】[Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 440  Solved: 242
[Submit][Status][Discuss]

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).php

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.ios

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

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

 
 

 

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

HINT

 

Source

Silverapp

sol:ide

这题怎么说呢 我都不知道他为啥是对的ui

首先咱们对b串作kmp 求出fail数组this

而后A串每次往里压一个字符 咱们能够发现有点相似dpspa

咱们考虑前边的串确定已经匹配完了 所以压进来的字符要是想匹配 那么必须和A串最后的一位接上debug

咱们记录tot[i]表示这一位已经匹配了多少

而后每次新来一个字符咱们就kmp一下

那么咱们如何kmp呢?

假如不匹配 咱们就直接往前跳fail就行了 可是记住答案是和fail有关的

/*In Search Of Life*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
char a[1000001];
char b[1000001];
char st[1000001];
int top;
int tot[1000001];
int fail[1000001];
void getfail()
{
    int k;
    fail[1]=0;
    for(int i=2;b[i];i++)
    {
        k=fail[i-1];
        while(b[k+1]!=b[i]&&k!=0)
        {
            k=fail[k];
        }
        if(b[k+1]==b[i])fail[i]=k+1;
    }
}
int main()
{
    scanf("%s",a+1);scanf("%s",b+1);
    getfail();
    int k,n=strlen(a+1),m=strlen(b+1);
    for(int i=1;i<=n;i++)
    {
        st[++top]=a[i];
        k=tot[top-1];
        while(st[top]!=b[k+1]&&k)
        {
            k=fail[k];
        }
        if(st[top]==b[k+1])k++;
        tot[top]=k;
        if(tot[top]==m)top-=m;
    }
    for(int i=1;i<=top;i++)putchar(st[i]);
    return 0;
}
View Code
相关文章
相关标签/搜索