题目链接: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. VC printf输出彩色字体

    在VC下使用SetConsoleTextAttribute()函数可以改变当前控制台的前景色和背景色,从而达到输出彩色字体的效果. 使用的方法也很简单,具体代码如下: #include <win ...

  2. 自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多

    实现SwipeRefreshLayout+RecycleView实现刷新 在你的xml文件里写上如下代码: <android.support.v4.widget.SwipeRefreshLayo ...

  3. linux vncserver配置

    需要使用Linux的远程桌面服务,xmanager之前用过,感觉一般,这次尝试下vnc 我的操作系统是centos7 一服务端 安装VNCServer #yum -y install vnc *vnc ...

  4. JavaScript单例模式

    一.什么是单例 意思是指获取的对象只有一份. 二.最通用的单例 任何时刻获取SingLeton.instance都是同一个对象 var SingLeton={ instance:{ property: ...

  5. html语言中的meta元素

    1.定义语言  格式:〈meta http-equiv=″Content-Type″ content=″text/html; charset=gb2312″〉  这是META最常见的用法,在制作网页时 ...

  6. Chart图形 [功能帮助类] Assistant创建显示图像的标签和文件 (转载)

    点击下载 Assistant.zip /// <summary> /// 类说明:Assistant /// 联系方式:361983679 /// 更新网站:[url=http://www ...

  7. angularjs中异常处理

    1.TypeError: Cannot read property '$valid' of undefined a. Add ng-submit attribute to the form: < ...

  8. BigDecimal类型的详情

    一.简介 1.概述 BigDecimal由任意精度的整数非标度值和32位的整数标度(scale)组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以10的负scale次幂. ...

  9. windows进程函数试炼

    实践一下windows进程相关函数: 代码如下: // test__getinformation.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&quo ...

  10. YII 数据库相关操作

    CDbConnection: 一个抽象数据库连接CDbCommand: SQL statementCDbDataReader: 匹配结果集的一行记录CDbTransaction:数据库事务 访问数据库 ...