BZOJ 3942: [Usaco2015 Feb]Censoring

Description

有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除.

Sol

KMP+栈.

用一个栈来维护新加的字符串就可以了..

一开始我非常的naive,写了个链表,只能过5个点,因为起始位置不太好搞...

Code

/**************************************************************
    Problem: 3942
    User: BeiYu
    Language: C++
    Result: Accepted
    Time:336 ms
    Memory:12032 kb
****************************************************************/
 
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
  
const int N = 1000005;
#define debug(a) cout<<#a<<"="<<a<<" "
  
char s[N],t[N];
int n,m,top;
int f[N],tp[N];
char stk[N];
  
int main(){
    scanf("%s",s+1);scanf("%s",t+1);
    n=strlen(s+1),m=strlen(t+1);
    for(int i=2,j=0;i<=m;i++){
        while(j && t[i]!=t[j+1]) j=f[j];
        if(t[i]==t[j+1]) ++j;
        f[i]=j;
    }
    for(int i=1,j=0;i<=n;i++){
        j=tp[top],stk[++top]=s[i];
        while(j && stk[top]!=t[j+1]) j=f[j];
        if(stk[top]==t[j+1]) ++j;
        tp[top]=j;
        if(j==m) top-=m;
    }
    stk[top+1]='\0';puts(stk+1);
    return 0;
}

  

posted @ 2016-10-30 20:40  北北北北屿  阅读(136)  评论(0编辑  收藏  举报