QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#717149 | #7894. Many Many Heads | shuanglin | WA | 0ms | 3784kb | C++14 | 3.0kb | 2024-11-06 17:03:28 | 2024-11-06 17:03:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lowbit(x) ((x) & - (x))
using PII = pair<int, int>;
using PIII = tuple<int, int, int>;
const int MOD = 1e9 + 7;
inline int mypow(int n, int k, int p = MOD) {
int r = 1;
for (; k; k >>= 1, n = n * n % p) {
if (k & 1) r = r * n % p;
}
return r;
}
struct cmp { //priority_queue
bool operator()(const PIII& a, const PIII& b) {
return std::get<0>(a) < std::get<0>(b); //down
}
};
bool cmpsort(const PII& a, const PII& b) { //sort
return a.first < b.first; //up
}
int n;
string str;
int ans[1000010] = { 0 };
deque< pair<int, char> >e;
int f = -1;
void cal(int l, int r) {
if (l >= r) {
return;
}
if ((str[l] == '(' || str[l] == ')') && (str[r] == '(' || str[r] == ')')) {
if (str[l] == ')' && str[r] == '(') {
f = 0;
return;
};
r--;
cal(l + 1, r);
}
else if ((str[l] == '[' || str[l] == ']') && (str[r] == '[' || str[r] == ']')) {
if (str[l] == ']' && str[r] == '[') {
f = 0;
return;
}
r--;
cal(l + 1, r);
}
else {
cal(l, ans[l]);
cal(ans[l + 1], r);
}
}
inline void solve() {
cin >> str;
str = " " + str;
n = str.size();
for (int i = 1; i < str.size(); i++) {
if (e.empty()) {
if (str[i] == '(' || str[i] == ')') e.push_back(make_pair(i, '('));
else e.push_back(make_pair(i, '['));
continue;
}
if (str[i] == '(' || str[i] == ')') {
if (e.back().second == '[') {
e.push_back(make_pair(i, '('));
}
else {
ans[e.back().first] = i;
e.pop_back();
}
}
else {
if (e.back().second == '(') {
e.push_back(make_pair(i, '['));
}
else {
ans[e.back().first] = i;
e.pop_back();
}
}
}
// for(int i=1;i<str.size();i++){
// cout<<ans[i]<<' ';
// }
for (int i = 1; i < str.size(); i++) {
if (ans[i] != 0) {
if (str[i] == '(' || str[i] == ')') {
str[i] = '(';
str[ans[i]] = ')';
}
else {
str[i] = '[';
str[ans[i]] = ']';
}
}
}
/*for (int i = 1; i < str.size(); i++) {
cout << ans[i] << ' ';
}
cout << endl;*/
int r = str.size() - 1;
f = 1;
cal(1, r);
if (f == 1) cout << "Yes" << '\n';
else cout << "No" << '\n';
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(12);
int t = 1;
cin >> t;
while (t--) {
solve();
for (int i = 0; i <= n + 5; i++) {
ans[i] = 0;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3784kb
input:
6 )) ((() [()] ()[()]() ([()]) ([])([])
output:
Yes No Yes No Yes No
result:
ok 6 token(s): yes count is 3, no count is 3
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3596kb
input:
2 (([([[([ ]]))])]])]
output:
Yes Yes
result:
wrong answer expected NO, found YES [2nd token]