題目描述
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());
}
}
}