1458:Seek the Name, Seek the Fame
#include"iostream"
#include<cstring>
using namespace std;
using ULL=unsigned long long;
const int P=131;
const int N=4e5+10;
ULL p[N],h[N];
char s[N];
void gethash()
{
int len=strlen(s+1);
p[0]=1,h[0]=0;
for(int i=1;i<=len;i++)
{
p[i]=p[i-1]*P;
h[i]=h[i-1]*P+s[i];
}
}
ULL get(int l,int r)
{
return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
while(~scanf("%s",s+1))
{
memset(p,0,sizeof p);
gethash();
int len=strlen(s+1);
for(int i=0;i<len;i++)
{
if(get(1,1+i)==get(len-i,len))
printf("%d ",i+1);
}
printf("\n");
}
return 0;
}
这篇文章展示了如何使用C++编程语言实现一个函数,通过计算字符串的哈希值来查找重复的字符。通过gethash()函数生成哈希表,get()函数用于比较子串哈希值,main()函数中遍历输入的字符串并输出重复字符的位置。
5万+

被折叠的 条评论
为什么被折叠?



