0

I've been trying to solve this question on SPOJ: http://www.spoj.com/problems/ONP/.

I've tried to implement a two Stack solution for the problem stated above. It works fine on my system, but I get a 'Wrong Answer' every time I try to submit the following code to the SPOJ Engine.

import java.io.*;
import java.util.Stack; 
import java.util.Scanner;


public class InfixToPostfix {

    public static String postfixString(String expression) {

        Stack <Character> valueStack = new Stack <Character>();
        Stack <Character> operatorStack = new Stack <Character>();

        char[] tokens = expression.toCharArray();

        for(char c : tokens) {

            if(c == '('){
                continue;
            }

            else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
                operatorStack.push(c);
                continue;
            }

            else if(c == ')') {
                valueStack.push(operatorStack.peek());
                operatorStack.pop();
                continue;
            }

            else {
                valueStack.push(c);
                continue;
            }
        }
        return valueStack.toString();
    }
    public static void main (String [] args)throws java.lang.Exception {

        String inputString = "";
        int n1;
        Scanner numberOfTestCases = new Scanner(System.in);
        try
        {
            n1 = numberOfTestCases.nextInt();
            StringBuilder[] sbuf = new StringBuilder[n1];
            Scanner inputExpression = new Scanner(System.in);

            for(int i = 0; i < n1; i++) {

                sbuf[i] = new StringBuilder();

                if(inputExpression.hasNextLine()) {

                    inputString = inputExpression.nextLine();

                    sbuf[i].append(postfixString(inputString).replaceAll("\\[", "").replaceAll("]", "").replaceAll(", ", ""));
                }
                else {
                    break;
                }
            }


            for(int i = 0; i < n1; i++) {
                System.out.println(sbuf[i].toString());
            }
        }
        catch (Exception e) {
           // System.out.println("");
           System.out.println(e.getMessage());  
            // numberOfTestCases.next();
        }
        System.exit(0);
    }

}

I can't figure out where I'm going wrong; I've tried all possible test cases.

P.S.: The problem assumes all inputs to be parenthesized; there's no need to include any code for resolving operator precedence.

user207421
  • 305,947
  • 44
  • 307
  • 483
AbhyudayaR
  • 51
  • 4

1 Answers1

0
return valueStack.toString();

This doesn't return the required format: it returns a comma-separated format bounded by [ and ], which isn't what is specified.

But I'm sure there are other problems.

  • I don't see anything in the problem statement that corresponds to your PS: on the contrary, it specifically mentions operator precedence.

  • Are you sure you've 'tried all possible test cases'? Try (1+2)/3 and 1+3/2.

  • You're peeking something you haven't pushed in the case of ')'.

  • I don't see why you need two stacks either.

Given the problem statement, what you need is either a recursive-descent expression parser or the Dijkstra Shunting-yard algorithm.

user207421
  • 305,947
  • 44
  • 307
  • 483