Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡀⠠⠤⠀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀
- ⠀⠀⠀⠀⣀⢤⡒⠉⠁⠀⠒⢂⡀⠀⠀⠀⠈⠉⣒⠤⣀⠀⠀⠀⠀
- ⠀⠀⣠⠾⠅⠈⠀⠙⠀⠀⠀⠈⠀⠀⢀⣀⣓⡀⠉⠀⠬⠕⢄⠀⠀
- ⠀⣰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡤⠶⢦⡀⠑⠀⠀⠀⠀⠈⢧⠀
- ⠀⡇⠀⠀⠀⠀⠀⢤⣀⣀⣀⣀⡀⢀⣀⣀⠙⠀⠀⠀⠀⠀⠀⢸⡄
- ⠀⢹⡀⠀⠀⠀⠀⡜⠁⠀⠀⠙⡴⠁⠀⠀⠱⡄⠀⠀⠀⠀⠀⣸⠀
- ⠀⠀⠱⢄⡀⠀⢰⣁⣒⣒⣂⣰⣃⣀⣒⣒⣂⢣⠀⠀⠀⢀⡴⠁⠀
- ⠀⠀⠀⠀⠙⠲⢼⡀⠀⠙⠀⢠⡇⠀⠛⠀⠀⣌⣀⡤⠖⠉⠀⠀⠀
- ⠀⠀⠀⠀⠀⠀⢸⡗⢄⣀⡠⠊⠈⢦⣀⣀⠔⡏⠀⠀⠀⠀⠀⠀⠀
- ⠀⠀⠀⠀⠀⠀⠈⡇⠀⢰⠁⠀⠀⠀⢣⠀⠀⣷⠀⠀⠀⠀⠀⠀⠀
- ⠀⠀⠀⠀⣠⠔⠊⠉⠁⡏⠀⠀⠀⠀⠘⡆⠤⠿⣄⣀⠀⠀⠀⠀⠀
- ⠀⠀⠀⠀⣧⠸⠒⣚⡩⡇⠀⠀⠀⠀⠀⣏⣙⠒⢴⠈⡇⠀⠀⠀⠀
- ⠀⠀⠀⠀⠈⠋⠉⠀⠀⢳⡀⠀⠀⠀⣸⠁⠈⠉⠓⠚⠁⠀⠀⠀⠀
- ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠓⠛⠛
- */
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cmath>
- #include <memory.h>
- #include <algorithm>
- #include <stack>
- #include <deque>
- #include <iomanip>
- #include <stdio.h>
- #include <queue>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <random>
- #include <ctime>
- #include <cstdlib>
- #define int long long
- #define pii pair <int, int>
- #define pb push_back
- #define all(vc) vc.begin(), vc.end()
- #define fir first
- #define sec second
- #define endl "\n"
- #define un unsigned
- #define INF 1000000009
- #define double long double
- using namespace std;
- const int N = 1000009;
- struct Hash
- {
- int h1, h2;
- Hash(int x, int y) : h1(x), h2(y) {}
- Hash(int x) : h1(x), h2(x) {}
- Hash() : h1(0), h2(0) {}
- };
- const Hash P = { 31, 37 }, MOD = { (int)1e9 + 7, (int)1e9 + 9 };
- vector<Hash> pw(N);
- Hash operator + (Hash a, Hash b)
- {
- return { (a.h1 + b.h1) % MOD.h1, (a.h2 + b.h2) % MOD.h2 };
- }
- Hash operator - (Hash a, Hash b)
- {
- return { (a.h1 - b.h1 + MOD.h1) % MOD.h1, (a.h2 - b.h2 + MOD.h2) % MOD.h2 };
- }
- Hash operator * (Hash a, Hash b)
- {
- return { (a.h1 * b.h1) % MOD.h1, (a.h2 * b.h2) % MOD.h2 };
- }
- Hash operator % (Hash a, Hash b)
- {
- return { a.h1 % b.h1, a.h2 % b.h2 };
- }
- Hash operator * (Hash a, int b)
- {
- return { (a.h1 * b) % MOD.h1, (a.h2 * b) % MOD.h2 };
- }
- bool operator != (Hash a, Hash b)
- {
- return a.h1 != b.h1 || a.h2 != b.h2;
- }
- bool operator == (Hash a, Hash b)
- {
- return a.h1 == b.h1 && a.h2 == b.h2;
- }
- void build_powers()
- {
- pw[0] = 1;
- for (int i = 1; i < N; i++)
- {
- pw[i] = pw[i - 1] * P;
- }
- return;
- }
- vector<Hash> build_hash(string &s)
- {
- vector<Hash> hs((int)s.size());
- hs[0] = 0;
- for (int i = 1; i < (int)s.size(); i++)
- {
- hs[i] = hs[i - 1] + pw[i] * s[i];
- }
- return hs;
- }
- Hash get_hash(vector<Hash>& hs, int l, int r)
- {
- return hs[r] - hs[l - 1];
- }
- void solve()
- {
- build_powers();
- int n;
- cin >> n;
- string s;
- cin >> s;
- string t = s;
- reverse(all(t));
- t = "0" + t;
- s = "0" + s;
- int p = 1;
- int cnt = 0;
- vector<Hash> hs = build_hash(s);
- vector<Hash> ht = build_hash(t);
- for (int i = 1; i <= n; i = p)
- {
- if (i + 1 <= n && s[i] == ')' && s[i + 1] == '(')
- {
- int j = i + 2;
- bool flag = 0;
- while (j <= n)
- {
- Hash a = get_hash(hs, i, j);
- a = a * pw[n - j + 1];
- Hash s = get_hash(ht, n - j + 1, n - i + 1);
- s = s * pw[i];
- if (a == s)
- {
- p = j + 1;
- flag = 1;
- cnt++;
- break;
- }
- else
- {
- j++;
- }
- }
- if (flag == 0)
- {
- cout << cnt << " " << n - p + 1 << endl;
- return;
- }
- }
- else if (i + 1 <= n)
- {
- p = i + 2;
- cnt++;
- }
- else
- {
- break;
- }
- }
- cout << cnt << " " << n - p + 1 << endl;
- return;
- }
- signed main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement