1458:Seek the Name, Seek the Fame

这篇博客探讨了在1458题中如何利用KMP算法解决字符串相关问题,介绍了KMP模板的使用,并分享了一个关于输入字符串的小技巧。

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

1458:Seek the Name, Seek the Fame

哎哎哎?
KMP的模板?
很显然我不会
不过我看了一下题解,貌似都是用KMP来做的
补充一个字符串的小常识
char s[SIZE]
while(~scanf("%s",s+1));//循环输入字符串
这种hash的题都很像,因为我们再维护hash值得时候直接把每一段进行一个维护就好了,然后做一个判断hash值,前缀后缀判断,输出即可

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
const int SIZE=400010;
const int base=41;
ull power[SIZE],hash[SIZE];
char s[SIZE];
int len;
bool check(int i)
{
	if(hash[i-1]*power[len-i+1]==hash[len]-hash[len-i+1]) return 1;
	return 0;
}
int main()
{
	power[0]=1;
	for(int i=1;i<=SIZE;i++)
	{
		power[i]=power[i-1]*base;
	}
	while(~scanf("%s",s+1))
	{
		len=strlen(s+1);
		memset(hash,0,sizeof(hash));
		for(int i=1;i<=len;i++)
		{
			hash[i]=hash[i-1]*base+(ull)(s[i]-'A'+1);//计算每一段的hash值 
		}
		for(int i=len;i>=1;i--)
		{
			if(check(i)) cout<<len-i+1<<" ";//倒序模拟反过来 
		}
		cout<<endl;
	}
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值