1、如果收到一个‘#’,删掉一个已经收到的字符,不包括‘#’;
2、如果收到一个‘@’,把收到的一整行删掉(‘#’和‘@’都是不可见字符)
输入:
3
whli##ilr#e(s#*s)
outcha@putchar(*s=#++)
returnWA##A!!##C
输出:
while(*s)
putchar(*s++)
returnAC
package 行编译器_队列法;
import java.util.*;
public class Main {
static String myCompile(String s){
char[] t=s.toCharArray();
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<t.length;i++){
if(t[i]=='#')
stack.pop();
else if(t[i]=='@')
stack.clear();
else
stack.push(new Character(t[i]));
}
int size=stack.size();
t=new char[size];
// Iterator<Character> iter=stack.iterator();//迭代器遍历相对于链表有优势,对于队列没优势
// int i=0;
// while(iter.hasNext()){
// t[i++]=iter.next();
// }
for(int i=size-1;i>=0;i--)
t[i]=stack.pop();
return new String(t);
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
//in.close();
in=new Scanner(System.in);
String t=null;
String[] out=new String[n];
long startTime;
long endTime;
startTime=System.currentTimeMillis();
for(int i=0;i<n;i++){
t=in.nextLine();
t=myCompile(t);
//System.out.println(t);
out[i]=t;
}
endTime=System.currentTimeMillis();
System.out.println("编译耗时:"+(endTime-startTime));
for(String i:out){
System.out.println(i);
}
}
}