1. 程式人生 > >LOJ #10036. 「一本通 2.1 練習 2」Seek the Name, Seek the Fame

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;
}