Getting exercises string 9 Chat solution to a problem

Written for: http://codeforces.com/problemset/problem/58/A

Title Description

Cong Cong recently learned to use the Internet.
So he entered a chat room, and we decided to play hello - said "hello".
But because Cong Cong fingers are not very flexible, so that he may touch some other keyboard playing "hello" in the process.
Cong Cong hit a string s.
For this string s, if we can remove some characters from s in (of course, can not be deleted) so that the rest of the characters without changing the order of exactly equal to "hello", then we can consider playing Cong Cong "hello".
For example:
if Cong Cong to fight a bunch of string "ahhellllloou", we can consider him a "hello";
and if Cong Cong to fight a bunch of string "hlelo", we think he did not play "hello . "
Please help make sure that we think Cong Cong hit "hello" or not fight.

Input Format

Input contains a string s, playing a character representation Cong Cong. Length of s will not exceed 100.

Output Format

For input string s Cong Cong, if we think he played "hello", the output "YES"; otherwise, output "NO".

Sample input 1

ahhellllloou

Sample output 1

YES

Sample input 2

hlelo

Sample output 2

NO

Topic analysis

We can open a variable cnt, cnt beginning equal to 0, then we have to traverse the string s.
For each traversed character c:
if c == 'h' && cnt == 0, cnt ++;
if c == 'e' && cnt == 1, cnt ++;
if c == 'l' && cnt == 2 | 3, cnt ++;
if c == '0' && cnt == 4, cnt ++;
if cnt> 4, directly outputs "YES" and exit;
if the end of the cnt <= 4, the output "NO".
Code is implemented as follows:

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

string s;
int n, cnt;

int main() {
    cin >> s;
    n = s.length();
    for (int i = 0; i < n; i ++) {
        char c = s[i];
        if (c == 'h' && cnt == 0) cnt ++;
        else if (c == 'e' && cnt == 1) cnt ++;
        else if (c == 'l' && (cnt == 2 || cnt == 3) ) cnt ++;
        else if (c == 'o' && cnt == 4) {
            puts("YES");
            return 0;
        }
    }
    puts("NO");
    return 0;
}

Guess you like

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