Analysis of past USACO bronze group real questions | February 2020 Mad Scientist

Learn C++ from a young age! Record the questions in the study process of preparing for the USACO (American Informatics Olympiad) bronze category competition and record every moment.

Attached is a summary post:Analysis of USACO past bronze group real questions | Summary-CSDN Blog


[Title description]

Farmer John's distant relative, Ben, is a mad scientist. Often this causes quite a bit of friction at family gatherings, but it can occasionally be beneficial, especially when Farmer John finds himself facing some unique and unusual problems with his cows.

Farmer John is currently facing a unique and unusual problem with her cows. He recently ordered N cows (1≤N≤1000), including two different breeds: Holsteins and Gensets. He specifies the cow in the order with a string of length N, where the characters are H (for Holsteins) or G (for racing cows). Unfortunately, when the cows arrived at his farm and he lined them up, their breed strings were different from the original ones.

We'll call these two strings A and B, where A is the string composed of the breed characters that Farmer John originally wanted, and B is the string composed of the characters of his cows when they arrive. Instead of simply checking to see if rearranging the cows in B would result in A, Farmer John asked his distant relative, Ben, to use his scientific talents to solve the problem.

After months of research, Ben invented an unusual machine: the Cow Breed Converter 3000, which can select any substring of cows and reverse their breed: all H's in this substring become G's, and all G becomes H. Farmer John wants to find the minimum number of uses of the machine required to change his current sequence B into the A he originally ordered. However, Ben's mad scientist skills don't handle anything beyond developing bizarre machines, so you'll need to help Farmer John solve this computational puzzle.

【enter】

The first line of input contains N, and the following two lines contain the strings A and B. Each string contains N characters, each of which is one of H and G.

【Output】

Output the minimum number of uses of the machine required to change B into A.

【Input sample】

7
GHHHGHH
HHGGGHH

【Output sample】

2

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int n, ans=0;
char a[1005], b[1005];
ifstream filein("breedflip.in");
ofstream fileout("breedflip.out");
int main()
{
    filein >> n;
    for (int i=1; i<=n; i++) {
        filein >> a[i];
    }
    for (int i=1; i<=n; i++) {
        filein >> b[i];
    }
    
    for (int i=1; i<=n; i++) {
        if (a[i]==b[i]) continue;
        for (int j=i; j<=n; j++) {
            if (a[j]!=b[j]) continue;
            else {
                ans++;
                i = j-1;
                break;
            }
        }
    }
    fileout << ans << endl;
    return 0;
}

【operation result】

7
GHHHGHH
HHGGGHH
2

おすすめ

転載: blog.csdn.net/guolianggsta/article/details/134848280