fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31.  
  32.  
  33. pair<int, vector<int>> consistency(int n){
  34.  
  35. vector<int> b(n);
  36. int br = 0;
  37. for(int i=0; i<n; i++){
  38. if(a[i] == '(') br++;
  39. else br--;
  40. b[i] = br;
  41. }
  42.  
  43. if(br != 0){
  44. return {-1, {}};
  45. }
  46.  
  47. vector<int> ans(n, 0);
  48. bool isColor1 = false;
  49. bool isColor2 = false;
  50.  
  51. int i = 0;
  52. while(i<n){
  53. while(i<n && b[i] >= 0){
  54. ans[i] = 1;
  55. isColor1 = true;
  56. i++;
  57. }
  58.  
  59. int j = i;
  60. while(j<n && b[j] <= 0){
  61. ans[j] = 2;
  62. isColor2 = true;
  63. j++;
  64. }
  65. i = j;
  66. }
  67.  
  68. if(isColor1 && isColor2){
  69. return {2, ans};
  70. }
  71.  
  72. ans.assign(n, 1);
  73.  
  74. return {1, ans};
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. pair<int, vector<int>> practice(int n){
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. void solve() {
  101.  
  102. int n;
  103. cin>> n;
  104.  
  105. cin >> a;
  106.  
  107. auto ans = consistency(n);
  108.  
  109. cout << ans.first << endl;
  110. if(ans.first != -1){
  111. for(auto& it : ans.second){
  112. cout << it << " ";
  113. }cout << endl;
  114. }
  115.  
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. int32_t main() {
  124. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  125.  
  126. int t = 1;
  127. cin >> t;
  128. while (t--) {
  129. solve();
  130. }
  131.  
  132. return 0;
  133. }
Success #stdin #stdout 0s 5312KB
stdin
5
8
((())))(
4
(())
4
))((
3
(()
18
((())())()))((())(
stdout
2
1 1 1 1 1 1 2 2 
1
1 1 1 1 
1
1 1 1 1 
-1
2
1 1 1 1 1 1 1 1 1 1 2 2 2 2 1 1 2 2