Competitive Problem Settingjava题解

题目描述

Chiaki经常给各种比赛出题,每场比赛需要的题目数从3到25不等,因此对题目的管理显得非常重要。

为了方便管理,Chiaki决定给每个题目取一个只有3个字符的编码。对于一个标题为ss的题目,它的编码是ss长度为3的前缀。

现在给出Chiaki出的nn个题的标题,求出这n个题的编码是否互不相同。

输入描述

输入有多组数据。第一行有一个整数T,表示测试数据组数。然后对于每组数据:

第一行包含一个整数n 3≤n≤25),表示题目数目。

接下来nn行,每行包含一个字符串s_i (3≤s i≤25),表示第i个题目的标题。保证s_i仅由英语小写字母组成。

保证所有数据中nn的和不超过1000。

输出描述

对于每组数据,如果这n个题目的编码互不相同,输出Yes,否则输出No。

样例输入 1

2
23
rectangles
tables
knight
dice
pawn
drilling
thewaytobytemountain
stamps
byteanteantowns
circulargame
permutation
quasitemplate
cakes
diamond
cards
fishes
chess
typetwodebruijnsequences
fibonaccimachine
colouring
programmingcontest
watchmen
tram
3
abcd
abce
cccd
样例输出 1

Yes
No
样例解释 1

对于第1个样例,所有长度为3的前缀都互不相同,所以输出是Yes。

对于第2个样例,前两个题目的前缀都是abc,所以输出是No。


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class c1876 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InputReader in = new InputReader(System.in);
		int T = in.nextInt();
		for(int i=0;i<T;i++)
		{
			boolean judge = true;
			int n = in.nextInt();
			List<String> list = new ArrayList();
			int j=0;
			for(j=0;j<n;j++)
			{
				String s = in.next();
				if(list.indexOf(s.substring(0,3))==-1)
				{
					list.add(s.substring(0,3));
				}
				else
				{
					judge = false;
					System.out.println("No");
					for(int k=j+1;k<n;k++)
					{
						s = in.next();
					}
					break;
				}
			}
			if(judge)
				System.out.println("Yes");
		}
		
	}
	static class InputReader{
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public InputReader(InputStream stream)
		{
			reader = new BufferedReader(new InputStreamReader(stream),32768);
			tokenizer = null;
		}
		public String next() {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			}
			catch(IOException e){
				throw new RuntimeException(e);
			}
			return tokenizer.nextToken();
		}
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值