Censoring S(板子)

题目描述

原题来自:USACO 2015 Feb. Silver

给出两个字符串 ,每次从前往后找到 的一个子串 并将其删除,空缺位依次向前补齐,重复上述操作多次,直到 串中不含 串。输出最终的 串。

输入格式

第一行包含一个字符串 ,第二行包含一个字符串

样例输入

whatthemomooofun
moo

样例输出

whatthefun

解析

\(KMP\) ,类似板子,字符串拼接,细节不少,代码可以多看看。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e7+6;
string s,t;
int p[N],n,m;

int main()
{
	cin>>s>>t;
	s=t+'#'+s;
	n=s.length(); m=t.length();
	for(int i=1;i<n;i++)
	{
		int j=p[i-1];
		while(j&&s[i]!=s[j]) j=p[j-1];
		p[i]=j+(s[i]==s[j]);
		if(p[i]==m&&i!=m-1) s.erase(i-m+1,m),i-=m;
	}
	s.erase(0,m+1);
	cout<<s;
	return 0;
}
posted @ 2024-05-03 21:19  ppllxx_9G  阅读(15)  评论(0编辑  收藏  举报