BZOJ 3942 Censoring

KMP。

怎么描述做法呢。。。“持久化”一下?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000050
using namespace std;
char s[maxn],t[maxn];
int l1,l2,nxt[maxn],pre[maxn],aft[maxn],ts[maxn];
bool flag[maxn];
void get_nxt()
{
    int cnt=0;
    for (int i=2;i<=l1;i++)
    {
        while (cnt && s[cnt]!=s[i-1]) cnt=nxt[cnt];
        if (s[cnt]==s[i-1]) cnt++;
        nxt[i]=cnt;
    }
    for (int i=0;i<=l2;i++) pre[i]=i-1,aft[i]=i+1;
    aft[l2-1]=-1;
}
void kmp()
{
    int cnt=0;
    for (int i=0;i<l2;i++)
    {
        while (cnt && s[cnt]!=t[i]) cnt=nxt[cnt];
        if (s[cnt]==t[i]) cnt++;
        ts[i]=cnt;
        if (cnt==l1)
        {
            int now=i;
            for (int j=1;j<=l1;j++)
            {
                flag[now]=true;
                now=pre[now];
            }
            cnt=ts[now];pre[i+1]=now;
        }
    }
}
int main()
{
    scanf("%s",t);l2=strlen(t);
    scanf("%s",s);l1=strlen(s);
    get_nxt();
    kmp();
    for (int i=0;i<l2;i++)
        if (!flag[i]) printf("%c",t[i]);
    printf("\n");
    return 0;
}
时间: 2025-01-18 10:32:00

BZOJ 3942 Censoring的相关文章

Bzoj 3942——Censoring(KMP)

传送门 前面一大串的英文题面被我忽略了 KMP+栈 只需通过维护一个栈就可以了(* ̄︶ ̄)(我懒得多写) 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdlib> 7 using namespace std; 8 char s1[1000005],s2[1

BZOJ 3670 &amp;&amp; BZOJ 3620 &amp;&amp; BZOJ 3942 KMP

最近感到KMP不会啊,以前都是背板的现在要理解了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxn=16000; 7 8 char S[Maxn]; 9 int k,P[Maxn],Ans; 10 11 12 inline void Kmp(char * S

BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 404  Solved: 221[Submit][Status][Discuss] Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material

BZOJ 3942 Usaco2015 Feb Censoring KMP算法

题目大意:给定两个串A和B,要求将A中删掉所有的B后输出 为何BC群刚有人问完我这题的[C++语法基础题]版之后就出了个KMP版的= = 维护一个栈,将A中的字符依次加进去,一旦A的栈顶出现了B就弹栈 用KMP算法来加速这个过程即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 using namespace st

BZOJ 3940 Censoring

把上题的KMP改成AC自动机. 注意root必须是0.因为root其实是NULL的意思. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxn 500050 using namespace std; char t[maxn],s[maxn]; int cases,l,son[maxn][30],f

Censoring(bzoj 3940)

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 inap

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB 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

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset

BZOJ 1013: [JSOI2008]球形空间产生器sphere

二次联通门 : BZOJ 1013: [JSOI2008]球形空间产生器sphere /* BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元 QAQ SB的我也能终于能秒题了啊 设球心的坐标为(x,y,z...) 那么就可以列n+1个方程,化化式子高斯消元即可 */ #include <cstdio> #include <iostream> #include <cstring> #define rg register #define Max