【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
相關文章
相關標籤/搜索