行編輯器

題目

你知道行編輯器嗎?不知道也沒關係,現在我會告訴你:
1.如果你收到一個’#’,那麼你應該刪掉一個你已經收到的字符,不包括’#’;
2.如果你收到一個’@’,那麼你應該把你收到的一整行都刪掉。
(‘#’和‘@’都爲不可見字符。)
你明白了嗎?現在輪到你去解決這個問題啦!
輸入
第一行是一個整數T,代表有T組數據。
每組數據的開始時一個字符串,字符串長度小於100,每個字符一定是(‘a’~’z’,’A’~’Z’,’*’,’!’,’(‘,’)’,’+’,’@’,’#’)中的一個
輸出
每組數據輸出一行經過行編輯器編輯過的字符串,具體可以看樣例

樣例輸入
3
whli##ilr#e(s#*s)
outcha@putchar(*s=#++)
returnWA##A!!##C
樣例輸出
while(*s)
putchar(*s++)
returnAC

代碼

public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);

        while(cin.hasNextLine()){
            String nStr = cin.nextLine();
            int n = Integer.parseInt(nStr);

            while(n!=0){
                n--;
                String str = cin.nextLine();
                Stack<Character> stack = new Stack<Character>();

                int len = str.length();
                for(int i=0; i<len; i++){
                    if(str.charAt(i) == '@'){
                        while(!stack.empty()) stack.pop();
                    }else if(str.charAt(i) == '#'){
                        if(!stack.empty()) stack.pop();
                    }else{
                        stack.push(str.charAt(i));
                    }
                }
                StringBuilder sb = new StringBuilder();
                while(!stack.empty()){
                    sb.append(stack.pop());
                }
                System.out.println(sb.reverse().toString());
            }
        }
        cin.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章