啦啦啦,这题题目有点长,概括一下就是,希望将所有()匹配的括号去掉
问你需要操作多少次
双指针,一个i一个j,从前往后记录匹配的括号
如果发现:
1. 括号匹配
2. i<j
ok,就放入ans
(⊙o⊙)…,最后记得sort一遍ans,第一遍因为这个wa了一发
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int>ans; for (int i = 0, j = s.size() - 1; i < j;) { while (s[i] == ')')i++; while (s[j] == '(')j--; if (s[i] == '(' && s[j] == ')' && i < j) { ans.push_back(i + 1); ans.push_back(j + 1); i++, j--; } else break; } if (ans.empty()) { puts("0"); } else { cout << 1 << endl; cout << ans.size() << endl; sort(ans.begin(), ans.end()); for (auto x : ans) { cout << x << ' '; } } return 0; }