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. sso系统使用

    一:什么是sso(single sign on) ? sso(单点登录系统)简单说就是客户端第一次访问应用1的时候,由于没有登录,会被引导到登录页面进行登录,如果登录校验通过,将返回一个认证信息tic ...

  2. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. 检查Json格式工具

    在线JSON校验格式化工具(Be JSON) 地址:http://www.bejson.com/

  4. Android两级嵌套ListView滑动问题的解决

    Android下面两级嵌套ListView会出现滑动失效,解决方案,把两级Listview全换成NoScrollListView,代码如下: public class NoScrollListView ...

  5. 基于jQuery的AJAX实现三级联动菜单

    最近学习jQuery,所以就写了一个关于中国省市县/区的三级联动菜单,权当相互学习,相互促进,特此记录. 下面是嵌套js的html文件: <!DOCTYPE html> <html ...

  6. mysql Access denied for user \'root\'@\'localhost\'” 本人解决方案:

    直接上图   昨天还是好的今天就不行了,密码是没错的,就是本地的连接不上,Linux上的mysql可以连, 网上找各种解决方案,什么权限,什么加一句话,还有这个 如果连这个都进不去那就直接重装吧,其实 ...

  7. Windows下对拍

    What is 对拍 Tool: 你的程序 可以输出正解的暴力程序 数据生成器 输出对比器 RP 用来干什么? 用来造数据,检验你的程序的正确性,以方便修改和出现未考虑到的情况 如何工作? 数据生成器 ...

  8. 华为专家谈CMDB建设

    CMDB成功的关键因素 对于CMDB项目的失败,普遍的解释是:没有数据的消费场景.工具和技术不行.流程管控不足. 从我自身的实践来看,我对此是有不同看法的.上述原因的确会影响人们使用CMDB,严重时甚 ...

  9. SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...

  10. 使用ssh keys实现免验证登陆远程服务

    使用ssh keys实现免验证登陆远程服务========================Created 星期四 10 五月 2018 引言------------------程序员或者服务器运维人员 ...