If there are "spikes" in two or more consecutive cells, then only the quantity of all previous coins needs to be counted; If there are no consecutive $2 $or more cells with spikes, it can be proven that we can definitely reach the last cell.
So this is the code:
#include <bits/stdc++.h>
using namespace std;
int sum = 0;
int main() {
int t;
cin >> t;
while (t--) {
int n, now = 0;
string s;
cin >> n >> s;
int sum = 0;
if(s.find("**") != string::npos) {
for(int i = 0; i < s.find("**"); i++) {
if(s[i] == '@') sum++;
}
cout << sum << endl;
}
else {
for(int i = 0; i < s.length(); i++) {
if(s[i] == '@') sum++;
}
cout << sum << endl;
}
}
return 0;
}
Auto comment: topic has been updated by optimize_ofast (previous revision, new revision, compare).
You could append "**" to the input string as a sentinel to simplify your code.
Look at the
solve()
function here.Thanks very much.