Don't like ads? PRO users don't see any ads ;-)
Guest

Phone List

By: a guest on May 12th, 2012  |  syntax: Java  |  size: 2.10 KB  |  hits: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2.  
  3. public class Main {
  4.     static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  5.    
  6.     private void quicksortTab(String[] tab, int startIndex, int endIndex) {
  7.         if (endIndex > startIndex) {
  8.                 String value = tab[endIndex];
  9.                
  10.                 int partition = partitionTab(tab, value, startIndex, endIndex - 1);
  11.                
  12.                 if (tab[partition].compareTo(value) < 0)
  13.                 ++partition;
  14.                
  15.                 swapTab(tab, partition, endIndex);
  16.  
  17.                 quicksortTab(tab, startIndex, partition - 1);
  18.                 quicksortTab(tab, partition + 1, endIndex);
  19.         }
  20.     }
  21.    
  22.     private int partitionTab(String[] tab, String value, int left, int right) {
  23.          while (left < right) {
  24.             if(tab[left].compareTo(value) < 0)
  25.                {++left; continue;}
  26.             if(tab[right].compareTo(value) >= 0)
  27.                {--right; continue;}
  28.             swapTab(tab, left, right); ++left;
  29.         }
  30.         return left;
  31.     }
  32.    
  33.     private void swapTab(String [] tab, int left, int right) {
  34.                 if(left == right) {
  35.                         return;
  36.                 }
  37.                
  38.                 String tmp = tab[left];
  39.                 tab[left] = tab[right];
  40.                 tab[right] = tmp;
  41.         }
  42.        
  43.         public static void main(String[] args) throws IOException {
  44.                 StreamTokenizer strTok = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  45.                 Main m = new Main();
  46.                
  47.                 int t; int n;
  48.                 boolean consistent;
  49.                
  50.                 String number;
  51.                
  52.                 strTok.nextToken();
  53.                 t = (int)strTok.nval;
  54.                
  55.                 for(int i=0;i<t;i++) {
  56.                         String [] phoneList = new String[10000];
  57.                         strTok.nextToken();
  58.                         n = (int)strTok.nval;
  59.                        
  60.                         for(int j=0;j<n;j++) {
  61.                                 strTok.nextToken();
  62.                                 number = Integer.toString((int)strTok.nval);
  63.                                 phoneList[j] = number;
  64.                         }
  65.                        
  66.                         consistent = true;
  67.                        
  68.                         m.quicksortTab(phoneList,0,n-1);
  69.                        
  70.                         for(int k=0;consistent && k<n;k++) {
  71.                                 if(phoneList[k+1] != null && phoneList[k+1].startsWith(phoneList[k]))
  72.                                         consistent = false;
  73.                         }
  74.                        
  75.                         if(consistent)
  76.                                 out.println("YES");
  77.                         else
  78.                                 out.println("NO");
  79.                         out.flush();
  80.                 }
  81.         }
  82. }