码家

Web Platform, Cloud and Mobile Application Development

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

http://pastebin.com/1CCUfPp3

 

 

  1. // Victor Padilla (vector9x@gmail.com) - SPOJ ONP
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class TransformExp {
  6.         static void dbg(Object...os) {
  7.                 System.err.println(Arrays.deepToString(os));
  8.         }
  9.         static BufferedReader input;
  10.         static StringTokenizer _stk;
  11.         static String readln() throws IOException {
  12.                 String l = input.readLine();
  13.                 if(l!=null) _stk = new StringTokenizer(l," ");
  14.                 return l;
  15.         }
  16.         static String next() { return _stk.nextToken(); };
  17.         static int nextInt() { return Integer.parseInt(next()); }
  18.         public static void main(String[] args) throws IOException {
  19.                 Locale.setDefault(Locale.US);
  20.                 input = new BufferedReader(new InputStreamReader(System.in));
  21.  
  22.                 readln();
  23.                 int numcases = nextInt();
  24.                 for(int caseid=1; caseid<=numcases; caseid++) {
  25.                         String exp = readln();
  26.                         for(char c : separadores)
  27.                                 exp = exp.replace(""+c, " "+c+" ");
  28.                        
  29.                         tknzr = new StringTokenizer(exp, " ");
  30.                         Node raiz = parser();
  31.                        
  32.                         salida =  new StringBuilder();
  33.                         raiz.postOrder();
  34.                         System.out.println(salida);
  35.                 }
  36.         }
  37.  
  38.         static StringTokenizer tknzr;
  39.         static final char[] separadores={'(',')','+','-','*','/','^'};
  40.        
  41.         static StringBuilder salida;
  42.        
  43.         static Node parser() {
  44.                 Node res = new Node();
  45.                 String tk = tknzr.nextToken();
  46.                 if(tk.equals("(")) { //caso recursivo, una subexpresion
  47.                         res.izq = parser(); //leo el primer operando, que es una expresion
  48.                         res.dato = tknzr.nextToken(); //leo el operador, +, -, ...
  49.                         res.der = parser(); //leo el segundo operando
  50.                         tknzr.nextToken(); //leo (y boto) el parentesis de cerrar
  51.                 }
  52.                 else { //caso trivial, una variable
  53.                         res.dato = tk;
  54.                 }
  55.  
  56.                 return res;
  57.         }
  58.        
  59.         static class Node {
  60.                 String dato;
  61.                 Node izq, der;
  62.                
  63.                 void postOrder() {
  64.                         if(izq!=null) { //se espera que tambien der!=null
  65.                                 izq.postOrder();
  66.                                 der.postOrder();
  67.                         }
  68.                         salida.append(dato);
  69.                 }
  70.         }
  71. }
posted on 2011-05-22 12:34  海山  阅读(213)  评论(0)    收藏  举报