题目描述:
//--------------------C_Style---------------- #include<stdio.h> #include<string.h> int main() { char word[100]; scanf("%s", word); int len = strlen(word); for(int i = 1; i <= len; ++i) if(len % i == 0) { int ok = 1; for(int j = i; j < len; ++j) if(word[j] != word[j%i]) { ok = 0; break; } if(ok) { printf("%d\n", i); break; } } return 0; }
//C++_Style //只需与第一个周期内的字符比较即可。 #include<iostream> using namespace std; int main() { string word; while(cin >> word) { int len = word.size(); for(int i = 1; i <= len; ++i) {//周期取值从1~len if(len % i == 0) { bool ok = true; for(int j = i; j < len; ++j) if(word[j] != word[j%i]) { ok = false; break; } if(ok) { cout << i << endl; break; } } } } return 0; }