每日编程-20170309

题目:如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。
输入描述:
输入包括n+1行:

第一行为单词个数n(1 ≤ n ≤ 50)

接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成


输出描述:
输出循环单词的种数

输入例子:
5
picture
turepic
icturep
word
ordw

输出例子:
2

解答:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 int main() {
 6     
 7     int number, count = 0, checked[50] = { 0 };  //count为答案,check为标记数组,表示对应位置的string已经被操作过。
 8     string sInput[50];
 9     cin >> number;
10     for (auto i = 0; i < number; i++)
11     {
12         cin >> sInput[i];
13     }
14     for (auto i = 0; i < number; i++)
15     {
16         if (checked[i] != 0)  //如果该string已经被操作过,则跳过该位置。
17         {
18             continue;
19         }
20         string sCompare1 = sInput[i] + sInput[i];  //将未标记的string加长一倍
21         count++;  //未被标记的新string可以将答案加1
22         checked[i] = 1;  //该string位置标记
23         for (auto j = i+1; j < number; j++)  //从加倍的string向后比较
24         {
25             if (checked[j] == 0 && sInput[i].size() == sInput[j].size() && sCompare1.find(sInput[j],0))  //如果遇到了1. 没有被标记。2. 长度相等。3.包含于sCompare的子串。
26             {
27                 checked[j] = 1;  //子串标记
28             }
29         }
30     }
31     cout << count << " ";
32 }

 第二版

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 int main() {
 6 
 7     int number, count = 0, checked[50] = { 0 };
 8     string sInput[50];
 9     cin >> number;
10     for (auto i = 0; i < number; i++)
11     {
12         cin >> sInput[i];
13     }
14     for (auto i = 0; i < number; i++)
15     {
16         if (checked[i] != 0)
17         {
18             continue;
19         }
20         string sCompare1 = sInput[i] + sInput[i];
21         count++;
22         checked[i] = 1;
23         for (auto j = i + 1; j < number; j++)
24         {
25             if (checked[j] == 0 
26                 && sInput[i].size() == sInput[j].size() 
27                 && sCompare1.find(sInput[j], 0) != string::npos)  
28                 //只修改了这里,上一版这里是错的,find返回的是sInput[j]位置,可以为0。
29                 //没找到返回string::npos。
30             {
31                 checked[j] = 1;
32             }
33         }
34     }
35     cout << count << " ";
36 }
原文地址:https://www.cnblogs.com/linhaowei0389/p/6527400.html