CodeForces 126B password KMP

本文介绍了一种用于判断字符串是否满足特定条件的算法:即在该字符串中能否找到一个子串,该子串同时作为字符串的前缀、后缀以及出现在字符串中间部分。文章通过具体的C++实现代码详细解释了这一过程。
题意:判断输入的字符串是不是符合下面的条件:在串中能找出一个子串,这个子串要求在前缀,后缀 及串中间都出现过,找到将其输出,没有则输出Just a Legent
#include<iostream>
#include<string>
using namespace std;
const int N = 1000001;
int Next[N],vis[N];
void getnext(string a)
{
	int k=-1,j=0;
	Next[0] = -1;
	while(j<a.size())
	{
		if(k==-1||a[j]==a[k])
		{
			j++;
			k++;
			Next[j] = k;
		}
		else 
		k=Next[k];
	}
}
int main()
{
	string s;
	cin>>s;
	getnext(s);
	for(int i=1;i<s.size();i++)
	vis[Next[i]]=1;
	int i = Next[s.size()];
	while(i)
	{
		if(vis[i])
		{
			for(int j=0;j<i;j++)cout<<s[j];
			cout<<endl;
			break;
		}
		else 
		i = Next[i];
	}
	if(i==0)cout<<"Just a legend"<<endl;
}

### 关于 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 来获取前五个与字符串处理相关的编程挑战链接,并打印出来供参考。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值