USACO 2022 US Open Contest Bronze Division - Photoshoot
Problem link: here
Solution Author: Stefan Dascalescu
Problem Solution
We only care about substrings of length two with different characters and do a bit of casework there.
Source codes
The source codes in C++ and Python can be seen below.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for(int i = n-1; i >= 0; i -= 2)
if(s[i] != s[i-1] && (cnt + s[i]) % 2 == 0)
cnt++;
cout << cnt;
return 0;
}
n = int(input())
s = input().strip()
cnt = 0
for i in range(n - 1, 0, -2):
if s[i] != s[i - 1] and (cnt + ord(s[i])) % 2 == 0:
cnt += 1
print(cnt)