思路:
构造题。首先把字符串变成“(((((...)))))”的样子,再根据k的取值变成“()()()...((...))”的样子即可。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int T; cin >> T; 6 while (T--) 7 { 8 int n, k; string s; cin >> n >> k >> s; 9 vector<pair<int, int>> res; 10 for (int i = 0; i < n / 2; i++) 11 { 12 if (s[i] == '(') continue; 13 int j = i + 1; 14 while (j < n && s[j] != '(') j++; 15 res.push_back(make_pair(i, j)); 16 reverse(s.begin() + i, s.begin() + j + 1); 17 } 18 for (int i = 0; i < k - 1; i++) 19 { 20 int cur = 2 * i + 1; 21 int j = cur + 1; 22 while (j < n && s[j] != ')') j++; 23 res.push_back(make_pair(cur, j)); 24 reverse(s.begin() + cur, s.begin() + j + 1); 25 } 26 cout << res.size() << endl; 27 for (auto it: res) 28 { 29 cout << it.first + 1 << " " << it.second + 1 << endl; 30 } 31 } 32 return 0; 33 }