如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。
输入描述:
输入包括n+1行: 第一行为单词个数n(1 ≤ n ≤ 50) 接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成输出描述:
输出循环单词的种数输入例子1:
5 picture turepic icturep word ordw输出例子1:
2#include <iostream> #include <vector> #include <string> using namespace std; bool Ischild(string str1, string str2) { int j = 0; for (int i = 0; i < str1.length(); i++) { if (str1[i] != str2[j]) { j = 0; } else { j++; if (j == str2.length()) return true; } } return false; } bool CompareStr(string str1, string str2) { if (str1.length() != str2.length()) return false; str1 = str1 + str1; bool result = false; result = Ischild(str1, str2); if (result) return true; return result; } int main() { int num; vector<string> str; vector<int> size; cin >> num; for (int i = 0; i < num; i++) { string tmp; cin >> tmp; str.push_back(tmp); size.push_back(0); } int count = 0; for (int i = 0; i < num; i++) { for (int j = i + 1; j < num; j++) { if (size[i] == 0) { if (CompareStr(str[i], str[j])) { size[i] = 1; break; } } } if (size[i] == 0) count++; } cout << count << endl; system("pause"); return 0; }
通过率为90%,待修改