You are not logged in. Please login at www.codechef.com to post your questions!

×

Compiler and Parsers

Why my code says WA i cannot get. Please Help

import java.io.*;



class Compiler

{

    static String a[];
    static int pos;

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

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(br.readLine());

        for(int z=0;z<t;z++) {
            a=new String[1000000];
            long l=0;
            pos=0;
            String s=br.readLine();
            for(int i=0;i<s.length();i++) {
                if(s.charAt(i)=='<')push(""+s.charAt(i));
                else {
                    if(pos>0) {
                        String k=pop()+">";
                        l=Math.max(l,k.length());
                        if(pos>0)a[pos-1]=a[pos-1]+k;
                    }
                }
            }
            System.out.println(l);


        }

    }
    static void push(String s) {
        a[pos++]=s;
    }
    static String pop() {
        String k=a[pos-1];
        a[pos-1]=null;
        pos--;
        return k;
    }

}
This question is marked "community wiki".

asked 21 May '14, 17:16

bradley's gravatar image

3★bradley
6562321
accept rate: 20%

edited 06 Jun '14, 13:51

admin's gravatar image

0★admin ♦♦
19.8k350498541


When ever you get

pos<0

shouldn't you end the loop? so i think you have to keep

(i<s.length())&&(pos>=0)

that might help.. i think so. (i don't know java, so i am not sure of the boolean condition)

link

answered 21 May '14, 17:46

eightnoteight's gravatar image

5★eightnoteight
1522415
accept rate: 0%

bro there is nothing called "pos<0" and this is working in all the test cases i have made and of the question.

(22 May '14, 14:49) bradley3★

then try these cases::

<><>><

<<<><<>>>

i checked your code in ideone.com, which gave wrong responses for these cases

(22 May '14, 19:55) eightnoteight5★

@eightnoteight for your first testcase the ans should be 2 or anything else?? and for the second it must be 8? what is the problem?

(13 Jun '14, 11:12) bradley3★

the first case should give 4, but your code gives 2

the second case should give 0, but your code gives 8

(14 Jun '14, 18:41) eightnoteight5★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×100
×4

question asked: 21 May '14, 17:16

question was seen: 938 times

last updated: 14 Jun '14, 18:41