ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1654. Cipher Message

I'm trying to use stack. It's true ! Why ?
Posted by TruongNT 22 Dec 2015 07:45
import java.util.Scanner;
import java.util.Stack;




public class CliperMessage_1654_Stack {
    public static void main(String[] arg1) {
        Scanner sc=new Scanner(System.in);
        StringBuilder sb=new StringBuilder();
        Stack<Character> stc=new Stack<>();
        sb.append(sc.nextLine());
        if(sb.length()<=200000){
            for(int i=0;i<sb.length();i++){
                if(stc.isEmpty()||(stc.peek()!=sb.charAt(i)))
                    stc.push(sb.charAt(i));
                else
                    if((stc.peek()==sb.charAt(i))) stc.pop();

            }

            StringBuilder sbn=new StringBuilder();
            int j=0;
            while(!stc.empty()) sbn.insert(j++, ""+stc.pop());
            System.out.println(sbn.reverse());
        }
    }
}