-1

I am submitting the following code to an online judge. Input Specification The data provided by Bill and Ted has the following format: The first line consists of the number p of parties followed by the number g of guesses (with 1 ≤ p ≤ 50 and 1 ≤ g ≤ 10000). Then follow p lines, each line consisting of a unique party name of length ≤ 20 (only containing letters a-z, A-Z and digits 0-9) and the achieved vote percentage of this party with one digit after the decimal point. After the parties follow g lines, each consisting of a guess. A guess has the form P1 + P2 + ... + Pk COMP n, where P1 to Pk are party names, COMP is one of the comparison operators <, >, <=, >= or = and n is an integer between 0 and 100, inclusively. Each party name occurs at most once in each guess.

Output Specification For each guess, sum up the vote percentages of the parties and compare them with the specified integer n. Then, print a line stating whether the guess was correct. See the sample output for details.

but whatever i do it always gives me time limit exceeded ...and here is my final code,

 public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String Ints =  br.readLine();
    String Guess;
    String [] Ints_array = Ints.split("[ ]+");
    int p = Integer.parseInt(Ints_array[0]);
    int g = Integer.parseInt(Ints_array[1]);
    HashMap<String, Float> Parties = new HashMap<String, Float>();

    for(int i = 0 ; i < p ; i++) {
        String temp = br.readLine();
        String [] temp_array = temp.split("[ ]+");
        Parties.put(temp_array[0],Float.parseFloat(temp_array[1]));
    }

    for(int j = 0 ; j < g ; j++) {
        Guess = br.readLine();
        float actual = 0;
          String [] temp = Guess.split("[ ]+");

        if(Guess.contains("+"))
        {

            for(int i = 0 ; i < temp.length-2 ; i++)
            {
                if(!temp[i].contains("+"))
                actual+= Parties.get(temp[i]);
            }
        }
        else
        {

            actual=Parties.get(temp[0]);
        }

        float guess = Float.parseFloat(temp[temp.length-1]);

                if( temp[temp.length-2].contains(">"))
               {
                   if(temp[temp.length-2].contains("="))
                   {
                       if(actual >= guess)
                         System.out.println("Guess #"+(j+1)+" was correct."); 
                    else
                        System.out.println("Guess #"+(j+1)+" was incorrect.");  
                   }
                   else
                   {
                    if(actual > guess)
                       System.out.println("Guess #"+(j+1)+" was correct."); 
                    else
                        System.out.println("Guess #"+(j+1)+" was incorrect."); 
                     }
               } 

                  else if( temp[temp.length-2].equals("<"))
               {
                   if(temp[temp.length-2].contains("="))
                   {
                       if(actual <= guess)
                         System.out.println("Guess #"+(j+1)+" was correct."); 
                    else
                        System.out.println("Guess #"+(j+1)+" was incorrect.");  
                   }
                   else
                   {
                    if(actual < guess)
                       System.out.println("Guess #"+(j+1)+" was correct."); 
                    else
                        System.out.println("Guess #"+(j+1)+" was incorrect."); 
                     }  
               } 

                    else if( temp[temp.length-2].equals("="))
               {
                    if(actual == guess)
                        System.out.println("Guess #"+(j+1)+" was correct."); 
                    else
                        System.out.println("Guess #"+(j+1)+" was incorrect.");  
               } 
    }

}

}

Sergey Shustikov
  • 13,329
  • 9
  • 57
  • 110

1 Answers1

0

I'll write a part of the answer, because I think that it would take a good search to have a complete answer.

Hope this would help.

Community
  • 1
  • 1
neo
  • 111
  • 12