
Phone List
By: a guest on
May 12th, 2012 | syntax:
Java | size: 2.10 KB | hits: 53 | expires: Never
import java.io.*;
public class Main {
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
private void quicksortTab(String[] tab, int startIndex, int endIndex) {
if (endIndex > startIndex) {
String value = tab[endIndex];
int partition = partitionTab(tab, value, startIndex, endIndex - 1);
if (tab[partition].compareTo(value) < 0)
++partition;
swapTab(tab, partition, endIndex);
quicksortTab(tab, startIndex, partition - 1);
quicksortTab(tab, partition + 1, endIndex);
}
}
private int partitionTab(String[] tab, String value, int left, int right) {
while (left < right) {
if(tab[left].compareTo(value) < 0)
{++left; continue;}
if(tab[right].compareTo(value) >= 0)
{--right; continue;}
swapTab(tab, left, right); ++left;
}
return left;
}
private void swapTab(String [] tab, int left, int right) {
if(left == right) {
return;
}
String tmp = tab[left];
tab[left] = tab[right];
tab[right] = tmp;
}
public static void main(String[] args) throws IOException {
StreamTokenizer strTok = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
Main m = new Main();
int t; int n;
boolean consistent;
String number;
strTok.nextToken();
t = (int)strTok.nval;
for(int i=0;i<t;i++) {
String [] phoneList = new String[10000];
strTok.nextToken();
n = (int)strTok.nval;
for(int j=0;j<n;j++) {
strTok.nextToken();
number = Integer.toString((int)strTok.nval);
phoneList[j] = number;
}
consistent = true;
m.quicksortTab(phoneList,0,n-1);
for(int k=0;consistent && k<n;k++) {
if(phoneList[k+1] != null && phoneList[k+1].startsWith(phoneList[k]))
consistent = false;
}
if(consistent)
out.println("YES");
else
out.println("NO");
out.flush();
}
}
}