首页 > 其他分享 >BZOJ 3942: [Usaco2015 Feb]Censoring KMP

BZOJ 3942: [Usaco2015 Feb]Censoring KMP

时间:2023-07-07 13:39:07浏览次数:37  
标签:字符 Usaco2015 ch Feb occurrence 后缀 3942 will include



3942: [Usaco2015 Feb]Censoring


Time Limit: 10 Sec  Memory Limit: 128 MB

Submit: 476  Solved: 260

[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


whatthemomooofunmoo


Sample Output


whatthefun




有两个S和T,有一空串U,然后从前往后枚举S串一个字符一个字符往U串里添加

若U串后缀为T,则去掉这个后缀继续流程。

很明显需要字符串匹配

如果每一次匹配成功就删掉 并重新匹配

复杂度会被卡成O(n^2)

所以呢

用一个栈来维护

只要记录一下每个字符被访问到时所匹配的长度即可

一旦被删除,只需要把已匹配数更新一下就好

复杂度重回线性


PS:

记得这题是当年ahcisy讲的例题。。。然而懒BJ怎么会去找,况且我当时不会。。。

今天开爷讲字符串

又有这个题,想了一想

wa我竟然会诶

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef double db;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
const int N=1000100;
int nt[N],pre[N];
char a[N],b[N],c[N];
int main()
{
	scanf("%s%s",a,b);
	int len=strlen(b);
	for(int i=1,j;i<len;i++)
	{
		j=nt[i];
		while(j&&b[i]!=b[j])j=nt[j];
		if(b[i]==b[j])nt[i+1]=j+1;
		else nt[i+1]=0;
	}
	int n=strlen(a);int top=-1;
	for(int i=0,j=0;i<n;i++)
	{
		c[++top]=a[i];
		while(j&&a[i]!=b[j])j=nt[j];
		if(a[i]==b[j])j++;else j=0;
		pre[top]=j;
		if(j==len)
		{j=pre[top-len];top=top-len;}
	}
	c[top+1]='\0';puts(c);
	return 0;
}
/*
whatthemomooofun
moo

whatthefun
*/




标签:字符,Usaco2015,ch,Feb,occurrence,后缀,3942,will,include
From: https://blog.51cto.com/u_16181403/6651987

相关文章

  • P3047 [USACO12FEB] Nearby Cows G
    #include<iostream>#include<vector>usingnamespacestd;constintN=100010,M=30;intn,m;intw[N];vector<int>g[N];intf[N][M],ans[N][M];voidDP1(intu,intfa){ for(inti=0;i<=m;i++)f[u][i]=w[u]; for(intx:g......
  • iText 0.30 - 0.99 (February 14, 2000 - May 1, 2003)
      ChangelogsiText0.30-0.99(February14,2000-May1,2003)iText0.30-0.99(February14,2000-May1,2003)In1998-1999,BrunowrotehisfirstPDFlibrary,butifyouwantedtouseit,youneededtobeaPDFspecialist:youneededtoknowallabout......
  • NC24727 [USACO 2010 Feb G]Slowing down
    题目链接题目题目描述EverydayeachofFarmerJohn'sN(1<=N<=100,000)cowsconvenientlynumbered1..Nmovefromthebarntoherprivatepasture.Thepasturesareorganizedasatree,withthebarnbeingonpasture1.ExactlyN-1cowunidirectional......
  • [USACO06FEB]Treats for the Cows G/S
    [USACO06FEB]TreatsfortheCowsG/S题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesoveragivenperiodtime.Th......
  • Luogu P4824 [USACO15FEB] Censoring S
    [USACO15FEB]CensoringS题面翻译FarmerJohn为他的奶牛们订阅了GoodHooveskeeping杂志,因此他们在谷仓等待挤奶期间,可以有足够的文章可供阅读。不幸的是,最新一期的文章包含一篇关于如何烹制完美牛排的不恰当的文章,FJ不愿让他的奶牛们看到这些内容。FJ已经根据杂志的所有文字,......
  • [刷题笔记] Luogu P3073 [USACO13FEB]Tractor S
    ProblemSolution和汽车拉力比赛差不多,思路都是二分,二分\(d\),但是汽车拉力比赛从一个路标开始搜即可,本题没有给定起点。一条合法路径起点是未知的,不得随便从一个点开始搜,否则可能找不到正确路径。怎么处理呢?容易想到对于每一个二分的\(d\),开一个\(n^2\)的循环,从每一个点开始搜......
  • Feb
    P9183[USACO23OPEN]FEB一道规律题$.....$说在前面的写这一道题,首先要知道一个东西:如果$A$的取值为($1,3,5$)中的一种,$B$的取值为($0,2,4$)中的一种,则$A+B$的取值为($1,3,5,7,9$)中的一种.如果$A$的取值为($1,2,3$)中的一种,$B$的取值为($0,2,4$)中的一种,则$A+B$的取值为($1,2,3,4,5,6......
  • P1676 [USACO05FEB] Aggressive cows G 题解
    题目传送门解题思路最大值最小化问题,考虑二分答案。首先要排序,保证序列单调不降,然后求出两个隔间之间的距离。sort(a+1,a+1+n);for(rii=1;i<=n;i++) dis[i]=a[i+1]-a[i];二分出一个\(mid\),判断它是否合法:每次累加距离,如果距离和比\(mid\)大,说明当前可以分配牛,记录数量......
  • Hungry Cow(USACO23 FEB Bronze T1)
    题目: 来写周练了,这道题目开开胃,就只用遍历一遍b数组、d数组再加上一些特判即可程序:#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10;longlongn,t,d[N],b[N];intmain(){ios::sync_with_stdio(false);cin>>n>>t;for(inti=1;i<=n;i++)......
  • 题解 P9130 【[USACO23FEB] Hungry Cow P】
    赛时开始一眼线段树分治,交了几发都T了,就意识到事情不对。后来想了想发现势能分析不能带撤销。。。后来加了一些不能改变复杂度假了的优化,没过之后就自闭跑路了。。。赛后听别人说了个楼房重建就明白怎么做了。首先,我们离线下来把\(a\)排序,去重(这样方便一点,不然权值线段树上......