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

Time limit exceeded on tes 8 (java), i dont know which way is excellent
Posted by Aydar 15 Aug 2011 12:54
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Stack;


public class problem1654 {

    public static void main(String[] args) throws IOException {
    BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
    String str=inp.readLine();
    Stack<Character> st=new Stack<Character>();
    st.push(str.charAt(0));
    String s="";
    for(int i=1;i<str.length();i++){
    if(!st.empty()&&st.peek()==str.charAt(i))
        st.pop();
    else{
    st.push(str.charAt(i));

    }
    }



while(!st.empty()){
    s=s+st.pop();
}
int n=s.length();
for(int i=0;i<n;i++){
    System.out.print(s.charAt(n-i-1));


    }

    }}
Re: Time limit exceeded on tes 8 (java), i dont know which way is excellent
Posted by Abzal 22 Aug 2011 19:23
When i used stack(from STL) in C++, i'd got TL8.
But after i realzed stack by using array, i got AC
Re: Time limit exceeded on tes 8 (java), i dont know which way is excellent
Posted by Moein Fatehi 14 Jan 2012 21:24
Don't use :
while(!st.empty()){
    s=s+st.pop();
}
--------
Use this phrase at the end of your Code:
Iterator<Character> b = st.iterator();
        while(b.hasNext()){
            System.out.print(b.next());
        }
Re: Time limit exceeded on tes 8 (java), i dont know which way is excellent
Posted by jim 21 Mar 2018 06:11
your reply help me very much!3Q