Skip to content

USACO 2020 February Contest Bronze Division - Mad Scientist#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Find positions where the first breach of rules occurs and then count the subsequent ones.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream cin("breedflip.in");
    ofstream cout("breedflip.out");

    int n;
    cin >> n;

    string a, b;
    cin >> a >> b;

    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
        if(a[i] != b[i] && (i == 0 || (a[i-1] == b[i-1])))
            cnt++;
    }

    cout << cnt;
    return 0;
}
with open("breedflip.in", "r") as fin:
    n = int(fin.readline().strip())
    a = fin.readline().strip()
    b = fin.readline().strip()
cnt = 0
for i in range(n):
    if a[i] != b[i] and (i == 0 or a[i-1] == b[i-1]):
        cnt += 1
with open("breedflip.out", "w") as fout:
    fout.write(str(cnt))