C. Messy(Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3))(思维)
Description
Input
4
8 2
()(())()
10 3
))()()()((
2 1
()
2 1
)(
Output
4
3 4
1 1
5 8
2 2
3
4 10
1 4
6 7
0
1
1 2
The meaning of problems
Give you one and a , give you a string of parentheses, to tell you the number of brackets must be paired.
You can have several operations, not the number, the inversion of a string.
Ask how you can string dubbed Operation
a "regular brackets", i.e. can be combined in parentheses
full. E.g:()()()
is equal to
;(()()())
is equal to
;(()())()
is equal to
。
Very simple question, first configure the required target symbol string , and then re-enumeration, to find the first one from behind , then invert . And knows all the same date
Code
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define m_p make_pair
#define _for(i, a) for(int i = 0; i < (a); ++i)
using namespace std;
int n, k;
vector< pair<int, int> > ans;
string s, t;
void init() {
ans.clear();
t.clear();
}
void sol() {
init();
cin >> n >> k;
cin >> s;
_for(i, k - 1) t += "()";
_for(i, n / 2 - k + 1) t += '(';
_for(i, n / 2 - k + 1) t += ')';
_for(i, n) {
if (s[i] != t[i]) {
int j = 0;
for (j = i + 1; s[j] != t[i]; j++);
reverse(s.begin() + i, s.begin() + j + 1);
//cout << i + 1 << " " << j + 1 << "\n";
ans.push_back(m_p(i + 1, j + 1));
}
}
cout << ans.size() << "\n";
for (pair<int, int> &i : ans) cout << i.first << " " << i.second << "\n";
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//freopen("in.txt", "r", stdin);
int T;
cin >> T;
_for(i, T) {
sol();
}
return 0;
}