HDU 3746

烦。。早上在写HDU 4338,发现细节太多了,写不了。原因其实是自己对TARJAN算法忘得差不多了。唉。。把之前想的一道循环节的题过了吧。。。再练练Tarjan,把4338也过了,加油。。。

#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;

char s[100010];
int next[100010];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%s",s);
		int len=strlen(s);
		next[0]=-1;
		int i=0,j=-1;
		while(i<len){
			if(j==-1||s[i]==s[j]){
				i++; j++;
				next[i]=j;
			}
			else{
				j=next[j];
			}
		}
		if(next[len]==0){
			printf("%d
",len);
			continue;
		}
		int p=len-next[len];
		if(len%p==0)
		printf("0
");
		else{
			printf("%d
",p-len%p);
		}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4132981.html