Pages

Thursday 7 February 2013

UVA - 10391 - Compound Words

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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("");
        String m="";
        HashSet<String> hs=new HashSet<String>();
        LinkedList<String> str=new LinkedList<String>();
        while((m=br.readLine())!=null){
           hs.add(m);
           str.add(m);
        }
        while(!str.isEmpty()){
            String temp=str.remove();
            StringBuilder tempS=new StringBuilder();
            boolean flag=false;
            for(int i=0;i<temp.length()-1;i++){
                tempS.append(temp.charAt(i));
                if(hs.contains(tempS.toString())){
                   StringBuilder  tempS2=new StringBuilder();
                   for(int j=i+1;j<temp.length();j++){
                       tempS2.append(temp.charAt(j));
                   }
                    if(hs.contains(tempS2.toString())){
                        flag=true;
                        break;
                    }
                }
            }
            if(flag){
                sb.append(temp).append("\n");
            }
        }
        System.out.print(sb);
    }
}

No comments:

Post a Comment