【題解】Codeforces 1392D. Omkar and Bed Wars

【題目敘述】http://codeforces.com/contest/1392/problem/D

#include <iostream>
using namespace std;
 
int t, n;
string s;
 
int main() {
    cin >> t;
    while (t--){
        cin >> n;
        cin >> s;
        if (s[0] == s[n-1]){
            int pos = 0;
            while (pos < n && s[pos] == s[0]) pos++;
            if (pos == n){
                cout << (n-1)/3+1 << "\n";
                continue;
            }
            s = s.substr(pos, n-pos)+s.substr(0, pos);
        }
        s = 'A'+s;
        int cnt = 1;
        int ans = 0;
        for (int i = 1; i <= n; i++){
            if (s[i] != s[i-1]){
                ans += cnt/3;
                cnt = 1;
            }
            else cnt++;
        }
        ans += cnt/3;
        cout << ans << "\n";
    }
}
分享本文 Share with friends