"Problem solution": string of incenses

Question A: string of incenses

Time limit: 1 Sec   Memory Limit: 512 MB

Face questions


He declined to publicly face the question.

answer


The string can eat starfish ...... positive solution KMP, but I use hash over 233,333 water.

Direct the string matching to see if split into smaller string. Enum length from small to large, not skip divisor, divisor is then matched.

Pretreatment and directly engage in a hash prefix would be finished.

Code:

 


			if(ch[i]!='A')te=0;
		if(te){printf("%lld\n",n*m-1);continue;}
		if(n*m<=1000000)
		{
			ans=0;JZ[0]=1;
			for(rint i=1;i<=n;++i)
				for(rint j=1;j<=m;++j)
				{
					rint lin=(i-1)*m+j;
					hs[lin]=hs[lin-1]*Z+(ch[j]-'A'+1);
					JZ[lin]=JZ[lin-1]*Z;
				}
			for(rint i=1;i<n*m;++i)
				if(hs[i]==hs[n*m]-hs[n*m-i]*JZ[i])ans=i;
			printf("%lld\n",ans);
			continue;
		}
		len=0,blo=0;
		for(rint i=1;i<=(m/2);++i)
		{
			if(m%i!=0)continue;_ok=1;
			for(rint j=2;j<=(m/i);++j)
				if((hs[i*(j-1)]-hs[i*(j-2)]*JZ[i])!=(hs[j*i]-hs[i*(j-1)]*JZ[i])){_ok=0;break;}
			if(_ok){len=i,blo=m/len;break;}
		}
		printf("%lld\n",(n-1)*m+(blo-1)*len);
	}
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11716492.html