题目链接: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. 5 Ways to Use Log Data to Analyze System Performance--reference

    Recently we looked across some of the most common behaviors that our community of 25,000 users looke ...

  2. C++数据结构和算法每天一练(线性表)

    #include <iostream> using namespace std;  class  ArrayLinerTable { public:       void InitLine ...

  3. Eclipse3.7中搭建Android开发环境文档教程和视频教程

    1.下载Eclipse3.7,登录http://www.eclipse.org/downloads/,下载Eclipse Classic 3.7: 2.安装ADT插件:下载好Eclipse后解压,运行 ...

  4. PHP表单验证内容是否为空

    内容为空效果图为: 填写内容效果图: 下面是验证程序的代码: <!doctype html> <html> <head> <meta http-equiv=& ...

  5. C#面向对象(二)

    一:抽象方法 1. 在面向对象编程语言中抽象方法指一些只有方法声明,而没有具体方法体的方法.抽象方法一般存在于抽象类或接口中. 在一些父类中,某些行为不是非常明确,因此无法用代码来具体实现,但是类还必 ...

  6. HDU-1113(map的运用)

    Word Amalgamation Problem Description In millions of newspapers across the United States there is a ...

  7. JDK1.8聚合操作

    在java8 JDK包含许多聚合操作(如平均值,总和,最小,最大,和计数),返回一个计算流stream的聚合结果.这些聚合操作被称为聚合操作.JDK除返回单个值的聚合操作外,还有很多聚合操作返回一个c ...

  8. 1,php概述

    学习了这么久的php,今天就跟着这本书,一章一章的去复习一下php的基础知识,个人理解如下:php是一门编写动态语言的web语言,能编写web语言的有好几种,但是人们都喜欢php,第一,php是开源的 ...

  9. T-SQL中的透视和逆透视

    透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣,每一种折扣只对应一个产品价格.下面贴出建表语句和插 ...

  10. JAVA 函数式接口与c#委托对应关系(一)

    C# Action委托 VS JAVA Action 接口函数 1.c#:Action 封装一个方法,该方法不具有参数并且不返回值. 构造实体类类 using System; namespace Ac ...