fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.print(decodeString("3[a]2[bc]"));
  13. }
  14. public static String decodeString(String s) {
  15. System.out.print(s);
  16. Stack<Character> ss = new Stack<>();
  17. Stack<String> bb = new Stack<>();
  18. boolean temp = false;
  19. int n = 1;
  20. System.out.println(n);
  21. String resl = "";
  22. for (Character c : s.toCharArray()) {
  23. if (c == '[') {
  24. n = Integer.parseInt(""+ss.pop());//循环次数
  25. temp = true;
  26. } else if (c == ']') {
  27. temp = false;
  28.  
  29. StringBuilder sb = new StringBuilder();
  30. StringBuilder res = new StringBuilder();
  31. for (String cc : bb) {
  32. sb.append(cc);
  33. }
  34. bb.clear();
  35. //sb.reverse();
  36. System.out.println(n);
  37. for (int i = 0; i < n; i++) {
  38. res.append(sb);
  39. }
  40. resl += res.toString();
  41. System.out.println(n);
  42. System.out.println(resl);
  43. n=0;
  44. } else {
  45. if (temp)
  46. bb.push("" + c);
  47. else
  48. ss.push( c);
  49. }
  50. }
  51. return resl;
  52.  
  53. }
  54. }
Success #stdin #stdout 0.12s 51684KB
stdin
Standard input is empty
stdout
3[a]2[bc]1
3
3
aaa
2
2
aaabcbc
aaabcbc