Exercises Chat room

Topic

Vasya has recently learned to type and log on to the Internet. He immediately entered a chat room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya managed to say hello if several letters can be deleted from the typed word so that it resulted in the word “hello”. For example, if Vasya types the word “ahhellllloou”, it will be considered that he said hello, and if he types “hlelo”, it will be considered that Vasya got misunderstood and he didn’t manage to say hello. Determine whether Vasya managed to say hello by the given word s.

Input

The first and only line contains the word s, which Vasya typed. This
word consisits of small Latin letters, its length is no less that 1
and no more than 100 letters.

Output

If Vasya managed to say hello, print “YES”, otherwise print “NO”.

Examples
Input

ahhellllloou

Output

YES

Input

program

Output

NO

Just saw this question, to think of a character for each input "hello" in it to make a mark, for example, enter 'h "after the make a mark, there is only entered after the tag' e 'is valid, and so on, behind the character is doing so, to conduct a final judgment, to see if the full input "hello".
the results debug code debugger for a long time, the sample is not life and death through ...
I enclose my Han Han Code

								**错误代码**
#include<iostream>
#include<string>
using namespace std;
int main()
{
	char ch;
	int flag = 0;
	while (cin >> ch)
	{
		if (ch == 'h' && flag == 0)
			flag++; 
		if (ch == 'e' && flag == 1)
			flag ++;
		if (ch == 'l' && flag == 2)
			flag ++;
		if (ch == 'l' && flag == 3)//输入一次‘l’会被运算两次,导致代码错误
			flag ++;
		if (ch == 'o' && flag == 4)
		{ 
			flag ++; 
			break;
		}
	}
	if (flag == 5)
		cout << "YES" << endl;
	if(flag!=5)
		cout << "NO" << endl;
	return 0;
}

By Gangster correction, the code is now clear that the problem lies

For this problem, another method may be used to do, string matching! ! ! !

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	int n,k=0;
	char str[5] = { 'h','e','l','l','o' };
	char a[105];
	cin >> a;
	n = strlen(a);
	for (int i = 0; i < n; i++)
	{
		if (a[i] == str[k])k++;
		if (k == 5)break;
	}
	if (k == 5)cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}

Do question should pay attention to the conversion of thinking, ah, really important. One way to nowhere, and start thinking about thinking from another angle, there will be a windfall Moe //

Published 14 original articles · won praise 18 · views 1666

Guess you like

Origin blog.csdn.net/SDAU_LGX/article/details/104760764
Recommended