optimize_ofast's blog

By optimize_ofast, history, 10 months ago, In English

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;
}
  • Vote: I like it
  • +5
  • Vote: I do not like it

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by optimize_ofast (previous revision, new revision, compare).

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You could append "**" to the input string as a sentinel to simplify your code.

Look at the solve() function here.