【USACO】Censoring(Silver)

本文介绍了一种基于KMP算法的变体方法,用于实现文本中特定子串的有效审查与删除。通过不断寻找并移除目标子串直至其不再出现,确保最终文本符合预设的内容审查标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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

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

Please help FJ determine the final contents of S after censoring iscomplete.

 输入

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 Twill be lower-case alphabet characters (in the range a..z).

 输出

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

 样例输入

whatthemomooofun

moo

样例输出

whatthefun

解题思路:这道题目是KMP的变形。将模版改一下即可。

代码:(请不要直接拷贝哦)

#include <cstdio>
#include <cstring>
char s[1000005],t[1000005],ans[1000005];
int next[1000005],pos[1000005];
int lens,lent,top=0;
using namespace std;
inline void getnext()//KMP的求next数组
{
	next[0]=-1;
	int i=0,j=-1;
	while (i<lent)
	  if ((j==-1)||(t[i]==t[j])) next[++i]=++j;
	    else j=next[j];
}
int main()
{
	scanf("%s",&s);
	scanf("%s",&t);
	lens=strlen(s);
	lent=strlen(t);
	getnext();
	int j=0;
	for (int i=0;i<lens;i++)
	{
		ans[top++]=s[i];//ans数组相当于是一个栈,用来将答案字母一个一个堆起来
		while ((j!=-1)&&(t[j]!=ans[top-1])) j=next[j];//不断地将字串往右挪
		if (t[j]==ans[top-1]) j++;//如果相同,可以比较下一个字符
		if (j==-1) j=0;//如果j还是-1的话,会影响到下面的程序,所以就将它改成0
		pos[top-1]=j;//记录当前这个字符已经匹配到子串的那个字符了
		if (j==lent) //子串是否成功完全匹配
		{//如果成功了
			top-=lent;//栈顶往下放,相当于去掉了这个子串
			j=pos[top-1];//回溯
		}
	}
	for (int i=0;i<top;i++)  putchar(ans[i]);
	printf("\n");
	return 0;
}

目前尚未有 USACO Silver 2025 March 的具体题目和官方题解发布,因为该时间点可能超出了当前已有的竞赛记录范围。然而,可以基于以往的 USACO Silver 级别的题目特点以及常见的算法知识点来推测潜在的内容。 USACO Silver 级别通常涉及的数据结构和算法包括但不限于:前缀和、滑动窗口、二分查找、贪心策略、并查集、BFS/DFS 和简单动态规划等[^3]。以下是关于如何准备类似级别比赛的一些建议: ### 常见问题类型分析 #### 数据处理与模拟 一些题目会测试选手对于复杂输入的理解能力以及基本逻辑实现的能力。例如,在某些场景下需要解析多组数据或者按照特定顺序执行操作。这类问题往往不需要复杂的理论基础,但要求程序具有良好的鲁棒性和效率。 #### 贪婪算法应用 当面对资源分配类的问题时,贪婪方法通常是首选解决方案之一。通过局部最优选择逐步构建全局最佳方案是一种常见思路。比如安排奶牛吃谷物的例子就体现了这一原则[^1]。 #### 图论基础知识 图遍历技术如广度优先搜索(BFS) 或者深度优先搜索(DFS),还有最短路径计算等问题也是Silver阶段的重要组成部分。拖拉机那道题就是一个很好的例子展示了如何利用双端队列优化传统BFS过程从而提高性能表现[^2]。 ```python from collections import deque def bfs_with_deque(start_node, adjacency_list): queue = deque([start_node]) visited = set() while queue: current = queue.popleft() if current not in visited: visited.add(current) for neighbor in adjacency_list[current]: if neighbor not in visited: queue.append(neighbor) return visited ``` ### 准备建议 为了更好地应对未来的USACO赛事,推荐定期练习过往真题,并深入理解每种核心概念背后的工作机制及其适用场合。同时也要注意培养代码调试技巧,这对于快速定位错误至关重要。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值