3942: [Usaco2015 Feb]Censoring

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 964 Solved: 480

[Submit][Status][Discuss]

Description

Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).

FJ has taken all of the text from the magazine to create the string S of length at most 10^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the first occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

Please help FJ determine the final contents of S after censoring is complete

有一个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

HINT

Source

Silver


KMP

建一个栈表示未被匹配的字符

每次只要看一看新加入的字符是否能接栈顶继续匹配,不行的话不断查找nex数组知道可以为止,

每次匹配长度达到c串长度时栈的深度-c串长度


#include<iostream>
#include<cstdio>
#define M 4000000
using namespace std; int la[M],i,m,n,j,k,a[M][2],nex[M],b[M],top,t;
char d[M],c[M]; int main()
{
scanf("%s%s",d+1,c+1); for(n=1;1;n++) if(d[n]<'a' || d[n]>'z') break;
for(m=1;1;m++) if(c[m]<'a' || c[m]>'z') break;
n-=1; c[m]='&'; m-=1; kmp(c); for(int i=2,j=0; i<=m;i++)
{
while(j && (c[i]!=c[j+1])) j=nex[j];
if(c[j+1]==c[i]) j+=1;
nex[i]=j;
} for(i=1;i<=n;i++)
{
t=a[top][0];
if(c[a[top][0]+1]==d[i]) t=a[top][0];
else while(t && d[i]!=c[t+1]) t=nex[t];
if(c[t+1]!=d[i]) t=-1;
top+=1;
a[top][0]=t+1; a[top][1]=i;
if(a[top][0]==m) top-=m;
}
for(i=1;i<=top;i++) printf("%c",d[a[i][1]]);
}

3942: [Usaco2015 Feb]Censoring的更多相关文章

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

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

  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. [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】

    题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...

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

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

  6. BZOJ 3940: [Usaco2015 Feb]Censoring

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

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

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

  8. bzoj3940: [Usaco2015 Feb]Censoring

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

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

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

随机推荐

  1. Java数据类型和不同数据类型在JVM内存分配

    1.java数据类型分类 Java语言是强类型(Strongly typed)语言,强类型包含两方面的含义:①所有的变量必须先声明,后使用:②指定类型的变量只能接受类型与之匹配的值.这意味着每个变量和 ...

  2. MVC 中文显示乱码问题

    在学习中遇到中文乱码的问题,在网上搜了一下大神的解决方法,总结了一点知识点. 项目中的代码 //GET: /HelloWorld/Welcome/ public string Welcome(stri ...

  3. 精尽 Kafka 学习指南【优秀学习指南汇总】

    1. 视频 炼石成金 <中间件之 Kafka> 一共有 19P .概念部分讲的蛮细的. 尚硅谷 <大数据视频_Kafka视频教程> 一共 24P .讲的还不错的. 书生小四 & ...

  4. Cocoa pod导入第三方框架遇到的那点事儿

    废话不多说,直接上干货. 以下所有操作均是在Cocoapod导入第三方库,并且是.xcworkspace的工程里面操作的, 1.导入头文件找不到,也就是所谓的,not find ''AFNetwork ...

  5. POJ3278(KB1-C 简单搜索)

    Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and wants ...

  6. CodeForces765C

    C. Table Tennis Game 2 time limit per test:2 seconds memory limit per test:512 megabytes input:stand ...

  7. Oracle用户权限及死锁

    Oracle用户权限表 oracle数据库中涉及到用户权限的三个表,dba_users,all_users,user_users有什么区别 dba_开头的是查全库所有的,all_开头的是查当前用户可以 ...

  8. 浏览器从输入到输出的过程与原理三之DNS

    1. DNS 在互联网上的每一个计算机都拥有一个唯一的地址,称作“IP地址”(即互联网协议地址).由于IP地址(为一串数字)不方便记忆,DNS允许用户使用一串常见的字母(即“域名”)取代.比如,您只需 ...

  9. Apache配置tomcat集群

     APACHE 2.2.9+TOMCAT6.0配置负载均衡 目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站,要达到以下要求: 1. Apache 做为 HttpSe ...

  10. c# 将秒数转换成时,分,秒的方法

    TimeSpan ts = , ,Convert.ToInt32( duration)); string str = ""; ) { str = ts.Hours.ToString ...