SPOJ上的NZEC运行时错误

时间:2014-08-09 05:17:08

标签: java runtime-error postfix-notation

我的代码在ideone上成功运行,但在SPOJ上显示NZEC运行时错误。有人可以解释我的代码有什么问题吗?

import java.util.*;
class tuna{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String[] str = new String[100];
        String pri = "+-*/^";

        Stack<String> stack = new Stack<String>();
        for(int i = 0; i<N; i++){
           str[i]=in.nextLine();
        }

    for(int i = 0; i<N; i++){
        for (int j = 0; j<str[i].length(); j++){
            char ch = str[i].charAt(j);
            if(Character.isLetter(ch))
                System.out.print(ch);
            else if(ch == '('){
                stack.push("(");
            }
            else if(ch == ')' )
            {
                while(!stack.isEmpty() && stack.lastElement()!="("){
                    System.out.print(stack.pop());

                }
                stack.pop();
            }
            else
            {
                while(!stack.empty() && stack.lastElement()!="(" && pri.indexOf(ch)<= pri.indexOf(stack.lastElement()))
                {
                    System.out.print(stack.pop());
                }
                stack.push(Character.toString(ch));
            }
        }
        while(!stack.isEmpty())
        {

            stack.pop();
        }
        System.out.println();
    }
    in.close();

  }
}

非常感谢对NZEC错误的一些见解。谢谢。 测试用例(在spoj上给出):

Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

1 个答案:

答案 0 :(得分:0)

从你的代码看。错误正在使用in.nextLine()。检查Using scanner.nextLine()

更正:更改为in.next()

 for(int i = 0; i<N; i++) {
     str[i]=in.next();
 }

我建议您使用BufferedReader代替Scanner for SPOJ问题以获得更好的效果