Advertisement
osipyonok

lab1v9

Nov 22nd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. //lab 1 v 9
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8. import java.util.*;
  9.  
  10. class UniqueChars{
  11.     public int cnt;
  12.     public String s;
  13.    
  14.     public UniqueChars(int _c , String _s){
  15.         cnt = _c;
  16.         s = _s;
  17.     }
  18. }
  19.  
  20. public class Lab1 {
  21.     public static void main(String[] args) {   
  22.         try{
  23.             Set<UniqueChars> list = new TreeSet<UniqueChars>(new Comparator<UniqueChars>() {
  24.                 public int compare(UniqueChars o1, UniqueChars o2) {
  25.                     return o1.s.compareTo(o2.s);
  26.                 }
  27.             });
  28.             BufferedReader bufferedReader = new BufferedReader(new FileReader("input.in"));
  29.             int cur = bufferedReader.read();
  30.             String s = "";
  31.             while (cur != -1) {
  32.                 char ch = (char)cur;
  33.                 if(IsSeparator(ch)){
  34.                     if(s.length() > 0){    
  35.                         int cnt = Count(s);
  36.                         if(list.size() > 0){
  37.                             if(list.iterator().next().cnt <= cnt){
  38.                                 if(list.iterator().next().cnt < cnt){
  39.                                     list.clear();
  40.                                 }
  41.                                 list.add(new UniqueChars(cnt , s));
  42.                             }
  43.                         }else{
  44.                             list.add(new UniqueChars(cnt , s));
  45.                         }
  46.                         s = "";
  47.                     }
  48.                 }else{
  49.                     s += ch;
  50.                 }
  51.                 cur = bufferedReader.read();
  52.             }
  53.             int cnt = Count(s);
  54.             if(s.length() > 0){
  55.                 if(list.size() > 0){
  56.                     if(list.iterator().next().cnt <= cnt){
  57.                         if(list.iterator().next().cnt < cnt){
  58.                             list.clear();
  59.                         }
  60.                         list.add(new UniqueChars(cnt , s));
  61.                     }
  62.                 }else{
  63.                     list.add(new UniqueChars(cnt , s));
  64.                 }
  65.             }
  66.             if(list.size() == 0){
  67.                 System.out.println("There is no words in the file");
  68.             }else{
  69.                 for(UniqueChars uc : list){
  70.                     System.out.println(uc.s);
  71.                 }
  72.             }
  73.         }catch(FileNotFoundException e){
  74.             System.out.print("File not found");
  75.         }catch (IOException e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.    
  80.     public static int Count(String s){
  81.         int cnt = 0;
  82.         boolean[] used = new boolean['z' + 1];
  83.         for(int i = 0 ; i < s.length() ; ++i){
  84.             if(used[s.charAt(i)] == false){
  85.                 used[s.charAt(i)] =  true;
  86.                 ++cnt;
  87.             }
  88.         }
  89.         return cnt;
  90.     }
  91.    
  92.     public static boolean IsSeparator(char ch){
  93.         if('a' <= ch && 'z' >= ch)return false;
  94.         if('A' <= ch && 'Z' >= ch)return false;    
  95.         return true;
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement