Pages

Tuesday 1 January 2013

UVA - 10815 - Andy's First Dictionary

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        HashSet<String> hs = new HashSet<String>();
        LinkedList<String> list = new LinkedList<String>();
        String m = "";
        while ((m = br.readLine()) != null) {
           /* if (m.equals("#")) {
                break;
            }*/
            StringBuilder temp = new StringBuilder();
            for (int i = 0; i < m.length(); i++) {
                if ((m.charAt(i) >= 'a' && m.charAt(i) <= 'z')
                        || (m.charAt(i) >= 'A' && m.charAt(i) <= 'Z')) {
                    temp.append(m.charAt(i));
                } else {
                    String word = temp.toString().toLowerCase();
                    temp = new StringBuilder();
                    if (word.equals("")) {
                        continue;
                    }
                    if (!hs.contains(word)) {
                        list.add(word);
                        hs.add(word);
                    }
                }
            }
            String word = temp.toString().toLowerCase();
            if (word.equals("")) {
                continue;
            }
            if (!hs.contains(word)) {
                list.add(word);
                hs.add(word);
            }
        }

        Collections.sort(list);
        while (!list.isEmpty()) {
            sb.append(list.remove()).append("\n");
        }
        System.out.print(sb);
    }
}

2 comments:

  1. if (m.equals("#")) {
    break;
    }

    Whats that for?

    ReplyDelete
    Replies
    1. Well .. it was just for testing the code ... It is not useful for the algorithm .. i will unquote it :)

      Delete