#include <iostream>
using namespace std;
int main()
{
string s;
while(getline(cin, s))
{
bool sw = true;
string ans = "";
string aux = "";
for (int i=0; i<s.length(); i++)
{
if (s[i] == ']')
sw = true;
else if (s[i] == '[')
{
sw = false;
ans = aux + ans;
}
else if (sw)
ans += s[i];
else if (!sw)
aux += s[i];
}
ans = aux + ans;
cout << ans << endl;
}
return 0;
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
StringBuilder sb = new StringBuilder();
boolean home = true;
String s = br.readLine();
List<Character> text;
List<Character> beijuT;
while(s != null)
{
text = new LinkedList<Character>();
beijuT = new LinkedList<Character>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ']')
home = true;
else if (s.charAt(i) == '[')
{
home = false;
}
else if (home)
text.add(s.charAt(i));
else if (!home)
beijuT.add(s.charAt(i));
}
for(Character ch: beijuT)
sb.append(ch);
for(Character ch: text)
sb.append(ch);
sb.append("\n");
s = br.readLine();
}
sb.deleteCharAt(sb.length()-1);
System.out.println(sb);
}
}
Need Help .... I am getting TLE
and i also know the idea that in c++ i could do it easily by using Iteraor.begin() and Iterator.end() method... if i want to do this kind of activity in java what the way can i do???
I'm enclosing links to the input / output that brianfry713 mentioned above and that I found very useful during testing / debugging. I'm unable to post the actual input and output here since it's too big.
In UNIX I would copy the sample input to 11988.in, and the sample output to 11988.out, compile your code using g++ to a.out, then you can run
./a.out < 11988.in
or
./a.out < 11988.in | diff - 11988.out
Check input and AC output for thousands of problems on uDebug!