Advertisement
ke_timofeeva7

Untitled

Mar 22nd, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. /*
  2. ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡀⠠⠤⠀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  3. ⠀⠀⠀⠀⣀⢤⡒⠉⠁⠀⠒⢂⡀⠀⠀⠀⠈⠉⣒⠤⣀⠀⠀⠀⠀
  4. ⠀⠀⣠⠾⠅⠈⠀⠙⠀⠀⠀⠈⠀⠀⢀⣀⣓⡀⠉⠀⠬⠕⢄⠀⠀
  5. ⠀⣰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡤⠶⢦⡀⠑⠀⠀⠀⠀⠈⢧⠀
  6. ⠀⡇⠀⠀⠀⠀⠀⢤⣀⣀⣀⣀⡀⢀⣀⣀⠙⠀⠀⠀⠀⠀⠀⢸⡄
  7. ⠀⢹⡀⠀⠀⠀⠀⡜⠁⠀⠀⠙⡴⠁⠀⠀⠱⡄⠀⠀⠀⠀⠀⣸⠀
  8. ⠀⠀⠱⢄⡀⠀⢰⣁⣒⣒⣂⣰⣃⣀⣒⣒⣂⢣⠀⠀⠀⢀⡴⠁⠀
  9. ⠀⠀⠀⠀⠙⠲⢼⡀⠀⠙⠀⢠⡇⠀⠛⠀⠀⣌⣀⡤⠖⠉⠀⠀⠀
  10. ⠀⠀⠀⠀⠀⠀⢸⡗⢄⣀⡠⠊⠈⢦⣀⣀⠔⡏⠀⠀⠀⠀⠀⠀⠀
  11. ⠀⠀⠀⠀⠀⠀⠈⡇⠀⢰⠁⠀⠀⠀⢣⠀⠀⣷⠀⠀⠀⠀⠀⠀⠀
  12. ⠀⠀⠀⠀⣠⠔⠊⠉⠁⡏⠀⠀⠀⠀⠘⡆⠤⠿⣄⣀⠀⠀⠀⠀⠀
  13. ⠀⠀⠀⠀⣧⠸⠒⣚⡩⡇⠀⠀⠀⠀⠀⣏⣙⠒⢴⠈⡇⠀⠀⠀⠀
  14. ⠀⠀⠀⠀⠈⠋⠉⠀⠀⢳⡀⠀⠀⠀⣸⠁⠈⠉⠓⠚⠁⠀⠀⠀⠀
  15. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠓⠛⠛
  16. */
  17.  
  18. #include <iostream>
  19. #include <string>
  20. #include <sstream>
  21. #include <cmath>
  22. #include <memory.h>
  23. #include <algorithm>
  24. #include <stack>
  25. #include <deque>
  26. #include <iomanip>
  27. #include <stdio.h>
  28. #include <queue>
  29. #include <map>
  30. #include <set>
  31. #include <unordered_map>
  32. #include <unordered_set>
  33. #include <random>
  34. #include <ctime>
  35. #include <cstdlib>
  36. #define int long long
  37. #define pii pair <int, int>
  38. #define pb push_back
  39. #define all(vc) vc.begin(), vc.end()
  40. #define fir first
  41. #define sec second
  42. #define endl "\n"
  43. #define un unsigned
  44. #define INF 1000000009
  45. #define double long double
  46. using namespace std;
  47.  
  48. const int N = 1000009;
  49.  
  50. struct Hash
  51. {
  52. int h1, h2;
  53.  
  54. Hash(int x, int y) : h1(x), h2(y) {}
  55. Hash(int x) : h1(x), h2(x) {}
  56. Hash() : h1(0), h2(0) {}
  57. };
  58.  
  59. const Hash P = { 31, 37 }, MOD = { (int)1e9 + 7, (int)1e9 + 9 };
  60.  
  61. vector<Hash> pw(N);
  62.  
  63. Hash operator + (Hash a, Hash b)
  64. {
  65. return { (a.h1 + b.h1) % MOD.h1, (a.h2 + b.h2) % MOD.h2 };
  66. }
  67.  
  68. Hash operator - (Hash a, Hash b)
  69. {
  70. return { (a.h1 - b.h1 + MOD.h1) % MOD.h1, (a.h2 - b.h2 + MOD.h2) % MOD.h2 };
  71. }
  72.  
  73. Hash operator * (Hash a, Hash b)
  74. {
  75. return { (a.h1 * b.h1) % MOD.h1, (a.h2 * b.h2) % MOD.h2 };
  76. }
  77.  
  78. Hash operator % (Hash a, Hash b)
  79. {
  80. return { a.h1 % b.h1, a.h2 % b.h2 };
  81. }
  82.  
  83. Hash operator * (Hash a, int b)
  84. {
  85. return { (a.h1 * b) % MOD.h1, (a.h2 * b) % MOD.h2 };
  86. }
  87.  
  88. bool operator != (Hash a, Hash b)
  89. {
  90. return a.h1 != b.h1 || a.h2 != b.h2;
  91. }
  92.  
  93. bool operator == (Hash a, Hash b)
  94. {
  95. return a.h1 == b.h1 && a.h2 == b.h2;
  96. }
  97.  
  98. void build_powers()
  99. {
  100. pw[0] = 1;
  101.  
  102. for (int i = 1; i < N; i++)
  103. {
  104. pw[i] = pw[i - 1] * P;
  105. }
  106.  
  107. return;
  108. }
  109.  
  110. vector<Hash> build_hash(string &s)
  111. {
  112. vector<Hash> hs((int)s.size());
  113. hs[0] = 0;
  114.  
  115. for (int i = 1; i < (int)s.size(); i++)
  116. {
  117. hs[i] = hs[i - 1] + pw[i] * s[i];
  118. }
  119.  
  120. return hs;
  121. }
  122.  
  123. Hash get_hash(vector<Hash>& hs, int l, int r)
  124. {
  125. return hs[r] - hs[l - 1];
  126. }
  127.  
  128. void solve()
  129. {
  130. build_powers();
  131.  
  132. int n;
  133. cin >> n;
  134.  
  135. string s;
  136. cin >> s;
  137.  
  138. string t = s;
  139. reverse(all(t));
  140.  
  141. t = "0" + t;
  142. s = "0" + s;
  143.  
  144. int p = 1;
  145. int cnt = 0;
  146.  
  147. vector<Hash> hs = build_hash(s);
  148. vector<Hash> ht = build_hash(t);
  149.  
  150. for (int i = 1; i <= n; i = p)
  151. {
  152. if (i + 1 <= n && s[i] == ')' && s[i + 1] == '(')
  153. {
  154. int j = i + 2;
  155. bool flag = 0;
  156.  
  157. while (j <= n)
  158. {
  159. Hash a = get_hash(hs, i, j);
  160. a = a * pw[n - j + 1];
  161. Hash s = get_hash(ht, n - j + 1, n - i + 1);
  162. s = s * pw[i];
  163.  
  164. if (a == s)
  165. {
  166. p = j + 1;
  167. flag = 1;
  168. cnt++;
  169. break;
  170. }
  171. else
  172. {
  173. j++;
  174. }
  175. }
  176.  
  177. if (flag == 0)
  178. {
  179. cout << cnt << " " << n - p + 1 << endl;
  180. return;
  181. }
  182. }
  183. else if (i + 1 <= n)
  184. {
  185. p = i + 2;
  186. cnt++;
  187. }
  188. else
  189. {
  190. break;
  191. }
  192.  
  193. }
  194. cout << cnt << " " << n - p + 1 << endl;
  195. return;
  196. }
  197.  
  198. signed main()
  199. {
  200. ios_base::sync_with_stdio(0);
  201. cin.tie(0);
  202. cout.tie(0);
  203.  
  204. int t;
  205. cin >> t;
  206.  
  207. while (t--)
  208. {
  209. solve();
  210. }
  211.  
  212. return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement