LOJ #10036. 「一本通 2.1 练习 2」Seek the Name, Seek the Fame

看题面戳我

脑抽了写了个map,没有发现多组数据为了卡n lg n,草率地加了一个while就愉快地TLE了

前置知识:双hash,一个hash总觉得不靠谱,所以双hash,不会的左转度娘

————————————————————————————————————————————

维护两个hash的数组,类似前缀和的操作

当然可以选择将数组改成变量(我就是这么做的)

这么简单的题卡了菜鸡这么久,菜鸡是不是很菜?

#include<cstdio>
#include<cstring>
#define ll long long
const int p1=1e9+7,p2=6662333;
using namespace std;

const int N=1e6+5;
char s[N];
int n,ans[N],top;
ll a1,a2,b1,b2,c1,c2;

int main()
{
	while(scanf("%s",s+1)!=EOF)
	{
		n=strlen(s+1);
		a1=a2=b1=b2=top=0; c1=c2=1;
		for(int i=1;i<=n;i++)
		{
			a1=(a1*1000+s[i])%p1,a2=(a2*1000+s[i])%p2;
			b1=(b1+c1*s[n-i+1])%p1,b2=(b2+c2*s[n-i+1])%p2;
			c1=c1*1000%p1,c2=c2*1000%p2;
			if(a1==b1&&a2==b2) ans[++top]=i;
		}
		for(int i=1;i<=top;i++) printf("%d%c",ans[i],i!=top?' ':'\n');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/YYHS_WSF/article/details/84256812