I have found a very short solution of 1997C - Even Positions compared to what's available everywhere. Please upvote if found helpful.
Shayan, adedalic, BledDest, Neon, awoo Is there any edge case for this solution?
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr);
void solve() {
int n; cin >> n;
string s; cin >> s;
int ans = 0;
for (int i = 0; i < n; i += 2) {
if (s[i + 1] == ')') {
ans += 1;
}
else {
ans += 3;
}
}
cout << ans << nl;
}
int main() {
FAST_IO
int tc; cin >> tc;
while (tc--) {solve();}
}
Auto comment: topic has been updated by amnchouhn (previous revision, new revision, compare).
Auto comment: topic has been updated by amnchouhn (previous revision, new revision, compare).
This is pretty clever! Starting from the end of the string helped me understand this approach -- the last character must be a ')', and from there you can have either -)-) or -(-). In the first case, you can do -)(), and ignore the last block () of size 2, adding 1 to the answer. Otherwise, for -(-) the next best thing to do is (()), adding 3 to the answer and ignoring the this last block of size 4. I think this solution works great!