BZOJ_3942_[Usaco2015 Feb]Censoring_KMP

Description

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

Input

The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

Sample Input

whatthemomooofun
moo

Sample Output

whatthefun

用一个栈来记录当前匹配到那个字符和那个字符在哪个位置。
然后KMP每次跳nxt即可。
 
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1000050
int nxt[N],ls,lw,a[N],pos[N];
char s[N],w[N];
void get_nxt() {
int i,j=0;
for(i=2;i<=lw;i++) {
while(j&&w[j+1]!=w[i]) j=nxt[j];
nxt[i]=(w[j+1]==w[i])?++j:0;
}
}
void work() {
int i,j,top=0;
for(i=1;i<=ls;i++) {
j=pos[top];
a[++top]=s[i];
while(j&&w[j+1]!=s[i]) j=nxt[j];
if(w[j+1]==s[i]) j++;
if(j==lw) {
top-=lw;
}else pos[top]=j;
}
for(i=1;i<=top;i++) printf("%c",a[i]);
}
int main() {
scanf("%s%s",s+1,w+1);
ls=strlen(s+1); lw=strlen(w+1);
get_nxt();
work();
}

BZOJ_3942_[Usaco2015 Feb]Censoring_KMP的更多相关文章

  1. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最小生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...

  2. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

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

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

  4. Bzoj3943 [Usaco2015 Feb]SuperBull

    3943: [Usaco2015 Feb]SuperBull Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 300  Solved: 185 Desc ...

  5. bzoj3940: [Usaco2015 Feb]Censoring

    AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...

  6. BZOJ_3940_[Usaco2015 Feb]Censoring_AC自动机

    BZOJ_3940_[Usaco2015 Feb]Censoring_AC自动机 Description FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个 ...

  7. 3942: [Usaco2015 Feb]Censoring

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

  8. 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树

    [BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...

  9. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...

随机推荐

  1. winform 写App.config配置文件——IT轮子系列(八)

    前言 在winform项目中,常常需要读app.config文件.如: var version = System.Configuration.ConfigurationManager.AppSetti ...

  2. Vector 特性

    1.Vector是一个连续内存占用的容器 2.每次Insert,都会将插入的对象先析构,然后复制一个副本添加到容器内 3.容器的大小先是1,然后是2,然后是4,也可以使用reserve来重新制定队列的 ...

  3. AngularJS数据绑定中数据监控的机制说明

    from : http://docs.angularjs.org/guide/scope When the browser calls into JavaScript the code execute ...

  4. git如何移除某文件夹的版本控制

    目录结构如下 project bin lib src ...... 执行如下的操作 git add . git commit -m "add bin/ lib/ src/" git ...

  5. 前端CDN公共库整理

    转自: 灰狼博客, 地址: http://itlobo.com/articles/2016.html 现在web应用都在使用js类库,这些类库小的几十K,大的几百K,而国内网络访问速度大家都知道不是那 ...

  6. Oracle VM VirtualBox(一) 快速拷贝一个新的虚拟机

    快速拷贝一个新的虚拟机 一 找到虚拟机vdi文件,复制一份并重命名. 二 修改新虚拟机的UUID 注:不修改创建不了虚拟机,会抛出Oracle VM VirtualBox UUID already e ...

  7. log4j配置及使用

    一.使用方法: 1.将log4j.properties放到你创建项目的src中 2.引入log4j.jar import org.apache.log4j.*; public class log4jT ...

  8. 如何在ES5与ES6环境下处理函数默认参数

    函数默认值是一个很提高鲁棒性的东西(就是让程序更健壮)MDN关于函数默认参数的描述:函数默认参数允许在没有值或undefined被传入时使用默认形参. ES5 使用逻辑或||来实现 众所周知,在ES5 ...

  9. 【转】Javascript全局变量var与不var的区别

    相信你对全局变量一定不陌生,在函数作用域里用a=1这种形式定义的变量会是一个全局变量,在全局作用域里,用下面3种形式都可以创建对全局可见的命名: <script> var a = 1; b ...

  10. System Error. Code:1722. RPC服务器不可用解决办法

    原文链接(转载请注明出处):System Error. Code:1722. RPC服务器不可用解决办法 问题 最近在软件设计上机课的时候,使用 starUML 建模工具画UML图的时候总是弹出一条如 ...