您的位置:首页 > 编程语言

[编程题] 循环单词

2017-08-02 10:26 260 查看
如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。
[b]输入描述:[/b]
输入包括n+1行:

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

接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成
[b]输出描述:[/b]
输出循环单词的种数

[b]输入例子1:[/b]
5
picture
turepic
icturep
word
ordw

[b]输出例子1:[/b]
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%,待修改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  牛客 c语言