Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Java代码TLE,求指点一下哪里还能再压缩时间,不理解啊import java.util.*; class node{ char s; node left; node right; node(char s, node l, node r) { this.s=s; left=l; right=r; } node(char s) { this.s=s; } } public class Main{ public static void main(String args[]) { Scanner cin=new Scanner(System.in); String qwe=cin.nextLine(); int num=Integer.parseInt(qwe); node [] st; node [] q; while(num-->0) { String str=cin.nextLine(); st=new node[10000]; int si=0; //build the tree for (int i=0;i<str.length();i++) { if (Character.isLowerCase(str.charAt(i))) { st[si++]=new node(str.charAt(i)); } else { node one=st[--si]; node two=st[--si]; st[si++]=new node(str.charAt(i), two, one); } } String result=""; node head=st[--si]; q=new node[10000]; int front=0; int tail=0; q[front++]=head; //traverse while(tail!=front) { node temp=q[tail++]; result+=temp.s; if (temp.left!=null) q[front++]=temp.left; if (temp.right!=null) q[front++]=temp.right; } //reverse int l=result.length(); for (int i=0;i<l;i++) System.out.print(result.charAt(l-1-i)); System.out.println(""); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator