import java.util.Scanner;
/**
* 如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。
* 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。
*
* @author pomay
*
*/
public class Nowcode_cycleword
{
public int cycleWordKind(String word[])
{
int count = 0;
boolean flag = false;
// 定义一个和输入单词个数相同的数组,用来统计这些单词中有多少种循环单词
String newStr[] = new String[word.length];
// 把第一个单词存入新数组
if (word != null)
newStr[0] = word[0];
for (int i = 1; i < word.length; i++)
{
// 从第二个新加入的开始判断该单词在不在新数组里
for (int j = 0; j < newStr.length; j++)
{
flag = compare(newStr[j], word[i]);
if (flag)
break;
}
// 如果不在新数组里,就添加进新数组
if (!flag)
newStr[i] = word[i];
}
// 在新数组中,有几个值就有几个不同的单词
for (int k = 0; k < newStr.length; k++)
{
if (newStr[k] != null)
count++;
}
return count;
}
// 判断两个单词是否相同,相同返回true,不同返回false
boolean compare(String s1, String s2)
{
boolean tag = false;
// 在s1后面再加一个s1,比如s1是happy,s就是happyhappy
String s = s1 + s1;
if (s1 == null || s2 == null || s1.length() != s2.length())
{
tag = false;
} else if (s.contains(s2))
// 如果s中包含了s2,比如s2是appyh就是happyhappy子串,以此来判断一个单词是否是另外一个单词的循环单词
tag = true;
return tag;
}
public static void main(String[] args)
{
// 输入包括n+1行:
Scanner s = new Scanner(System.in);
// 第一行为单词个数n(1 ≤ n ≤ 50)
int n = s.nextInt();
// 接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成
String word[] = new String[n];
for (int i = 0; i < n; i++)
{
word[i] = s.next();
}
Nowcode_cycleword cycleword = new Nowcode_cycleword();
int num = cycleword.cycleWordKind(word);
System.out.println(num);
}
}
牛客之统计n个单词中有多少种循环单词
最新推荐文章于 2020-08-06 15:45:54 发布