Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lab 1 v 9
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.*;
- class UniqueChars{
- public int cnt;
- public String s;
- public UniqueChars(int _c , String _s){
- cnt = _c;
- s = _s;
- }
- }
- public class Lab1 {
- public static void main(String[] args) {
- try{
- Set<UniqueChars> list = new TreeSet<UniqueChars>(new Comparator<UniqueChars>() {
- public int compare(UniqueChars o1, UniqueChars o2) {
- return o1.s.compareTo(o2.s);
- }
- });
- BufferedReader bufferedReader = new BufferedReader(new FileReader("input.in"));
- int cur = bufferedReader.read();
- String s = "";
- while (cur != -1) {
- char ch = (char)cur;
- if(IsSeparator(ch)){
- if(s.length() > 0){
- int cnt = Count(s);
- if(list.size() > 0){
- if(list.iterator().next().cnt <= cnt){
- if(list.iterator().next().cnt < cnt){
- list.clear();
- }
- list.add(new UniqueChars(cnt , s));
- }
- }else{
- list.add(new UniqueChars(cnt , s));
- }
- s = "";
- }
- }else{
- s += ch;
- }
- cur = bufferedReader.read();
- }
- int cnt = Count(s);
- if(s.length() > 0){
- if(list.size() > 0){
- if(list.iterator().next().cnt <= cnt){
- if(list.iterator().next().cnt < cnt){
- list.clear();
- }
- list.add(new UniqueChars(cnt , s));
- }
- }else{
- list.add(new UniqueChars(cnt , s));
- }
- }
- if(list.size() == 0){
- System.out.println("There is no words in the file");
- }else{
- for(UniqueChars uc : list){
- System.out.println(uc.s);
- }
- }
- }catch(FileNotFoundException e){
- System.out.print("File not found");
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static int Count(String s){
- int cnt = 0;
- boolean[] used = new boolean['z' + 1];
- for(int i = 0 ; i < s.length() ; ++i){
- if(used[s.charAt(i)] == false){
- used[s.charAt(i)] = true;
- ++cnt;
- }
- }
- return cnt;
- }
- public static boolean IsSeparator(char ch){
- if('a' <= ch && 'z' >= ch)return false;
- if('A' <= ch && 'Z' >= ch)return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement