题目链接:BZOJ - 3942

题目分析

我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T 。

所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹配。

那么我们维护一个栈,一直向后匹配,如果栈顶出现了 T ,就弹出 T 个字符,然后继续从新的栈顶向后匹配就可以了。

匹配使用 KMP 算法。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath> using namespace std; const int MaxL = 1000000 + 5; int l, lt, Top;
int Next[MaxL], f[MaxL]; char S[MaxL], T[MaxL], Str[MaxL]; void Prepare()
{
int j = Next[1] = 0;
for (int i = 2; i <= lt; ++i)
{
while (j > 0 && T[j + 1] != T[i]) j = Next[j];
if (T[j + 1] == T[i]) ++j;
Next[i] = j;
}
} int main()
{
scanf("%s%s", S + 1, T + 1);
l = strlen(S + 1); lt = strlen(T + 1);
Prepare();
int j = 0;
for (int i = 1; i <= l; ++i)
{
Str[++Top] = S[i];
while (j > 0 && T[j + 1] != Str[Top]) j = Next[j];
if (T[j + 1] == Str[Top]) ++j;
if (j == lt)
{
Top -= lt;
j = f[Top];
}
else f[Top] = j;
}
Str[Top + 1] = 0;
printf("%s\n", Str + 1);
return 0;
}

  

[BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】的更多相关文章

  1. bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】

    好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...

  2. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

  3. BZOJ 3942: [Usaco2015 Feb]Censoring

    Description 有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除. Sol KMP+栈. 用一个栈来维护新加的字符串就可以了.. 一开始我非常的 ...

  4. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  5. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  6. BZOJ 3940: [Usaco2015 Feb]Censoring

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 367  Solved: 173[Subm ...

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

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has ...

  8. BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  9. bzoj 1355: [Baltic2009]Radio Transmission【kmp】

    kmp复健,答案是n-next[n] #include<iostream> #include<cstdio> using namespace std; const int N= ...

随机推荐

  1. [Redux] Wrapping dispatch() to Log Actions

    We will learn how centralized updates in Redux let us log every state change to the console along wi ...

  2. iOS swift使用xib绘制UIView

    目标:用xib绘制一个UIView,在某个ViewController中调用. 三个文件:ViewController.Swift    DemoView.swift     DemoView.xib ...

  3. IOS8开发之实现App消息推送

    第一部分 Apple Push Notification Service 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps:其实每一篇教程都有),先来看一张苹果官方对其推送做出解释的概要图. ...

  4. 3DES加解密【示例】

    代码 /**  * 3DES加解密  */ public class DESedeUtils {     private static final String ALGORITHM_MD5 = &qu ...

  5. mysql锁表和解锁语句分享

    对于MySQL来说,有三种锁的级别:页级.表级.行级   页级的典型代表引擎为BDB.  表级的典型代表引擎为MyISAM,MEMORY以及很久以前的ISAM.  行级的典型代表引擎为INNODB.  ...

  6. js去掉所有空格

    <script> String.prototype.trim = function () {            return this.replace(/(^\s*)|(\s*$)/g ...

  7. svn出错问题(用户名密码有修改以及资源url改变时)

    用eclipse 同步SVN服务器宛然无法访问了: org.tigris.subversion.javahl.ClientException: RA layer request failed svn: ...

  8. android中ListView控件

    今天学习了ListView控件和页面跳转,下面大致介绍下: 第一步:创建显示内容的文件vlist.xml: <?xml version="1.0" encoding=&quo ...

  9. python基础知识十

    特殊的方法 在类中有一些特殊的方法具有特殊的意义,比如__init__和__del__方法,它们的重要性我们已经学习过了. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[k ...

  10. declare-styleable:自定义控件的属性

    http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html 在使用过程中, 1 TypedArray a = getContext() ...