Problem string 3_4 uva 455 cycles

This problem because the data is relatively small, it is possible to direct violence
to enumerate its length from 1-n, if it succeeds, it would go directly out,

#include <cstdio>
#include <cstring> 
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		char s[100];
		scanf("%s",s);
		int m = strlen(s), j = 1;
		while(j <= m)
		{
			if(!(m%j)) // 你要周期,至少得是它的倍数八 
			{
				int ok = 1; // 标记是否可以 
				for (int i = 1; i < m/j;i++) // 次数 
				{
					for (int k = 0; k < j; k++) // 周期长度 
					{
						if(s[k]!=s[i*j+k])
						{
							ok = 0;
							break;
						}
					}
					if(!ok) break;
				}
				if(ok) 
				{
					printf("%d\n",j);
					break;
				}
			}
			j++;
		}
		if(n) printf("\n");
	}
	return 0;
} 
Published 55 original articles · won praise 1 · views 2647

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/100553606