CodeForces 625B War of the Corporations(KMP)

本文介绍了一个有趣的字符串处理问题,即如何通过最小化更改使两个公司的产品名称不发生冲突。使用KMP算法解决这一挑战,避免因相似名称引起的法律纠纷。

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


B. War of the Corporations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.

This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence.

Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn't contain the name of the phone as a substring.

Substring is a continuous subsequence of a string.

Input

The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters.

Output

Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring.

Examples
input
intellect
tell
output
1
input
google
apple
output
0
input
sirisiri
sir
output
2
Note

In the first sample AI's name may be replaced with "int#llect".

In the second sample Gogol can just keep things as they are.

In the third sample one of the new possible names of AI may be "s#ris#ri".


题意:

没怎么读懂。

思路:

模式串匹配,上个周才学的,这个周试下。

AC:

#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e5;

char S[MYDD], T[32];
int next[32];
void Getnext(int l) {
	next[1] = 0;
	int i = 1, j = 0;
	while(i < l) {
		if(j == 0 || T[i] == T[j]) {
			++i;
			++j;
			next[i] = j;
		} else
			j = next[j];
	}
//	for(int j = 1; j <= l; j++)
//		printf("next[%d] -> %d\n", j, next[j]);
}

int kmp(int ls, int lt, int pos) {
	int ans = 0;
	while(pos <= ls) {
		int i = pos, j = 1;
		while(i <= ls && j <= lt) {
			if(j == 0 || S[i] == T[j]) {
				i++;
				j++;
			} else {
				j = next[j];
			}
		}
		if(j > lt) {
			ans++;
		}
		pos = i;
	}
	return ans;
}

int AC {
//	while(1) {
	int ls, lt;
	scanf("%s", S+1);
	scanf("%s", T+1);
	ls = strlen(S+1);
	lt = strlen(T+1);
	Getnext(lt);
	printf("%d\n", kmp(ls, lt, 1));


//		if(kmp(strlen(S+1), strlen(T+1)))   puts("yes");
//		else
//			puts("no");
//	}
	return 0;
}


### 关于 Codeforces 平台上的 KMP 算法练习题目 对于希望在 Codeforces 上找到有关 KMP (Knuth-Morris-Pratt) 字符串匹配算法的练习题目的选手来说,可以关注几个特定标签下的问题。通常这些题目会被标记为 "strings" 或者更具体地标记为 "string suffix structures"[^1]。 为了帮助更好地理解如何查找适合的练习题目,在此提供一段 Python 代码用于模拟访问 API 获取带有指定标签的问题列表: ```python import requests def fetch_problems(tag, platform="codeforces"): url = f"https://{platform}.com/api/problemset.problems?tags={tag}" response = requests.get(url) if response.status_code != 200: raise Exception(f"Failed to retrieve data from {platform}") json_data = response.json() problem_list = [] for index, item in enumerate(json_data['result']['problems']): name = item["name"] rating = item.get('rating', 'N/A') contest_id = item["contestId"] index = item["index"] problem_info = { "Name": name, "Rating": rating, "Link": f"{platform}.com/contest/{contest_id}/problem/{index}" } problem_list.append(problem_info) return problem_list[:5] # Example usage with the tag related to string processing or specifically KMP pattern matching. kmp_related_tag = "strings" fetched_problems = fetch_problems(kmp_related_tag) for prob in fetched_problems: print(prob) ``` 这段脚本会调用 Codeforces 的公开 API 来获取前五个与字符串处理相关的编程挑战链接,并打印出来供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值