#include #include #include /** * Periodic String by Steven Zeil */ using namespace std; string rotate (string s) { string result = s; unsigned n = s.size(); for (unsigned i = 0; i < n; ++i) { result[(i+1)%n] = s[i]; } return result; } void processDataset (istream& in, string s) { for (unsigned k = 1; k <= s.size(); ++k) { if (s.size() % k == 0) { string component = s.substr(0, k); unsigned pos = k; bool OK = true; for (unsigned pos = k; OK && (pos < s.size()); pos += k) { component = rotate(component); OK = s.substr(pos, k) == component; } if (OK) { cout << k << endl; return; } } } cout << "Something is very, very wrong." << endl; } void solve (istream& in) { string s; in >> s; processDataset (in, s); } /* * Run the program. Because not all IDEs support easy redirection of * input, if one or more command line parameters are supplied, * they are treated as the name of an input file. Otherwise, read from cin. */ int main(int argc, char** argv) { if (argc > 1) { for (int i = 1; i < argc; ++i) { ifstream input (argv[i]); solve (input); } } else { solve (cin); } return 0; }