首页 > 试题广场 >

循环单词

[编程题]循环单词
  • 热度指数:10300 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。

输入描述:
输入包括n+1行:
第一行为单词个数n(1 ≤ n ≤ 50)
接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成


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

输入

5
picture
turepic
icturep
word
ordw

输出

2
示例2

输入

4
goran
igor
domagoj
relja

输出

4

说明

并不是必须包含两个或两个以上的不同单词才算一种循环单词!  
import java.util.*;
public class Main {
    /*
    善用Set会比较简单。
    思路就是如果通过右移char,会与set中的其他元素重复,remove之,最终的set集合大小就是所求的结果
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.nextLine());
            // 同时使用数组和set来存储输入的字符串
            String[] strings = new String[n];
            HashSet<String> resultSet = new HashSet<>();
            for (int i = 0; i < n; i++) {
                strings[i] = in.nextLine();
                resultSet.add(strings[i]);
            }
            for (int i = 0; i < n; i++) {
                // 用StringBuilder来处理字符串
                StringBuilder sb = new StringBuilder(strings[i]);
                for (int j = 0; j < sb.length(); j++) { // 遍历移动char的方式
                    // 如果set中的该元素还没有被移除,则进入处理,否则直接跳过
                    if (resultSet.contains(strings[i])) {
                        sb.append(sb.charAt(0));
                        sb.deleteCharAt(0);
                        // 删去set中同一种循环单词,同时防止把自己删了=.=
                        if (resultSet.contains(sb.toString()) && !sb.toString().equals(strings[i])) {
                            resultSet.remove(sb.toString());
                        }
                    }
                }
            }
            System.out.println(resultSet.size());
        }
    }
}

发表于 2018-03-03 17:46:27 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<String> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			list.add(sc.next());
		}
		
		System.out.println(getCycWordNum(list));
	}
	
	/*
	 *1、将列表第一个单词叠加,并在后续列表中遍历是否是当前单词的循环单词
	 */
	public static int getCycWordNum(ArrayList<String> list){
		if(list.size() == 0)
			return 0;
		
		boolean flag = false;
		int count = 0;
		while(!list.isEmpty()){
			String word = list.get(0);
			word = word+word;
			for (int i = 0; i < list.size(); i++) {
				if(word.contains(list.get(i))&&list.get(i).length() == word.length()/2){
					flag = true;
					list.remove(i);
					i--;//remove只有数组数量减少,i为之前的i+1个元素
				}
			}
			if(flag){
				count++;
				flag = false;
			}
		}
		return count;
	}
}

发表于 2017-09-02 12:29:10 回复(0)
public class MainTest {
public static void main(String[] args) {
int N=50;
       Scanner sc = new Scanner(System.in);
       N = sc.nextInt(); //第一行为单词个数N
       ArrayList<String> list = new ArrayList<>();
       int count = 0;
       for (int i = 0; i < N; i ++) {
           String s = sc.next(); //输入并存储每行的单词。此处必须调用next()方法
           if (!list.contains(s)) {
               count ++;
               list.add(s);
               for (int j = 0; j < s.length() - 1; j ++) {
                   //以下注释是另一种方法,把可能的循环单词加入list,
                   //思路:把要测试的单词后再重复下这个单词,如:picture ,变成 picturepicture
                   String str= new String();
                   str=s.concat(s);
                   String b= str.substring(j, s.length() + j);
                   list.add(b);
               }
           }
       }
       System.out.println(count);
   }
}

编辑于 2017-06-05 21:24:32 回复(0)
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        /**
         * 该解法思想就是,把每个单词所有可能的循环单词都放在一个数据结构中,
         * 可以是List,map,set,数组等等,此处用ArrayList.
         * 然后判断下一个单词是否在表中,不在则加入,并把循环种类加1
         * 在的话,则它与之前的单词是同一种循环单词
         */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); //第一行为单词个数n(1 ≤ n ≤ 50)
        ArrayList<String> list = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < n; i ++) {
            String s = sc.next(); //输入并存储每行的单词。此处必须调用next()方法,不能是nextLine()方法
            if (!list.contains(s)) {
                count ++;
                list.add(s);

                for (int j = 0; j < s.length() - 1; j ++) {
                    char last = s.charAt(s.length() - 1);
                    s = s.substring(0, s.length() - 1);
                    s = last + s;
                    list.add(s);
                    //以下注释是另一种方法,把可能的循环单词加入list,
                    //思路:把要测试的单词后再重复下这个单词,如:picture ,变成 picturepicture
                    //感谢得闲半生的idea
//                    StringBuffer string = new StringBuffer();
//                    string.append(s);
//                    string.append(s);
//                    String another = string.substring(j, s.length() + j);
//                    list.add(another);
                }
            }
        }
        sc.close();
        System.out.println(count);
    }
}


编辑于 2017-03-12 21:28:31 回复(22)

热门推荐

通过挑战的用户