9.3 Exercise 5 words covering restore solution to a problem

Written for: Luo Gu P1321

Title Description

A length \ (l (3 \ le l \ le 255) \) string is repeatedly affixed boyand girltwo words, after pasting the cover may be affixed with a word (not represented covered with dots), the final word each have at least one character is not covered. I asked the boy several affixed with a few girl?

Input Format

Line repeatedly been labeled with strings boy and girl two words.

Output Format

Two lines, two integers. Behavior boy first number, second number girl behavior.

Sample input

......boyogirlyy......girl.......

Sample Output

4
2

problem analysis

Basic simulation questions. You can be solved by finding the law:
suppose to be our string s. Because the topics tells us that every boy and girl will not completely covered, so we only need to determine:

  • If \ (s [i] == ' b' \) or \ (s [i + 1] == 'o' \) or \ (S [I + 2] == 'Y' \) , then the original \ (s [i] \) is the "boy" in the "b", then the number of boys plus 1;
  • If \ (s [i] == ' g' \) or \ (s [i + 1] == 'i' \) or \ (s [i + 2] == 'r' \) or \ (S [I +. 3] == 'L' \) , then the original \ (s [i] \) is the "girl" to "g", then add a quantity of the girl.

We boy_num and girl_num to indicate the number of boys and girls.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int boy_num, girl_num;
string s;
int main() {
    cin >> s;
    int n = s.length();
    for (int i = 0; i < n; i ++) {
        if (s[i] == 'b' || i+1 < n && s[i+1] == 'o' || i+2 < n && s[i+2] == 'y') boy_num ++;
        if (s[i] == 'g' || i+1 < n && s[i+1] == 'i' || i+2 < n && s[i+2] == 'r' || i+3 < n && s[i+3] == 'l') girl_num ++;
    }
    cout << boy_num << "\n" << girl_num << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11570748.html