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. // your code goes here
  13. MyScanner sc = new MyScanner();
  14. String s = sc.nextLine();
  15. int len = s.length();
  16. char[] arr = new char[len];
  17. int last = -1;
  18. for (int i = 0; i < len; ++i){
  19. arr[i] = s.charAt(i);
  20. if (arr[i] == '#'){
  21. last = i;
  22. }
  23. }
  24. if (arr[len-1] == '(' || arr[0] != '('){
  25. System.out.println(-1);
  26. return;
  27. }
  28. boolean ok = true;
  29. int k = 0;
  30. for (int i = len-1; i >=last; --i){
  31. if (arr[i] == '('){
  32. k--;
  33. }else if (arr[i] == ')'){
  34. k++;
  35. }
  36. if (k < 0){
  37. ok = false;
  38. break;
  39. }
  40. }
  41.  
  42. if (!ok){
  43. System.out.println(-1);
  44. return;
  45. }
  46. int maxR = 0;
  47. int minL = 0;
  48. List<Integer> res = new ArrayList<Integer>();
  49.  
  50. for (int i = 0; i < len; ++i){
  51. if (arr[i] == '('){
  52. maxR++;
  53. }else if (arr[i] == ')'){
  54. minL++;
  55. }else{
  56. if (maxR <= minL){
  57. ok = false;
  58. break;
  59. }
  60. if (i != last){
  61. res.add(1);
  62. minL++;
  63. }else{
  64. res.add(maxR - minL - k);
  65. minL = maxR - k;
  66. if (minL < 1){
  67. ok = false;
  68. break;
  69. }
  70. }
  71. }
  72. if (maxR < minL){
  73. ok = false;
  74. break;
  75. }
  76. }
  77. if (minL != maxR){
  78. ok = false;
  79. }
  80.  
  81. if (ok){
  82. for (int i = 0; i < res.size(); ++i){
  83. System.out.println(res.get(i));
  84. }
  85. }else{
  86. System.out.println(-1);
  87. }
  88. }
  89.  
  90. //-----------PrintWriter for faster output---------------------------------
  91. public static PrintWriter out;
  92.  
  93. //-----------MyScanner class for faster input----------
  94. public static class MyScanner {
  95.  
  96. public MyScanner() {
  97. }
  98.  
  99. String next() {
  100. while (st == null || !st.hasMoreElements()) {
  101. try {
  102. st = new StringTokenizer(br.readLine());
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. return st.nextToken();
  108. }
  109.  
  110. int nextInt() {
  111. return Integer.parseInt(next());
  112. }
  113.  
  114. long nextLong() {
  115. return Long.parseLong(next());
  116. }
  117.  
  118. double nextDouble() {
  119. return Double.parseDouble(next());
  120. }
  121.  
  122. String nextLine(){
  123. String str = "";
  124. try {
  125. str = br.readLine();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. return str;
  130. }
  131.  
  132. }
  133. }
Success #stdin #stdout 0.05s 4386816KB
stdin
((((#)(())(()
stdout
-1