Power Strings
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 38038 | Accepted: 15740 |
Description
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
题目分析:给你一个字符串,求出这个字符串的最小循环节的个数,如果该串没有子循环节,就只有自己本身一个循环节,当然结果就是1啦!
像ababab:循环节就是ab,共有3个ab; 像abc:循环节就是abc,只有本身一个。
code:
#include <stdio.h> #include <string.h> char s[1000002]; int main() { int len; while(scanf("%s", s)!=EOF) { if(s[0]=='.') break; len = strlen(s); for(int i=1; i<=len; i++) if(len%i==0) { int ok = 1; for(int j=i; j<len; j++){ if( s[j] != s[j%i] ) { ok = 0; break; } } if(ok!=0){ printf("%d ", len/i); break; } } } return 0; }