牛客之统计n个单词中有多少种循环单词

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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值