[CF从零单排#12] 58A - Chat room

题目来源:http://codeforces.com/problemset/problem/58/A

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
hlelo
output
NO

题目大意:

给定一个字符串s,长度不超过100,只含有小写字母。如果这个字符串中,能通过删掉一些字符,最后得到"hello"这个单词,那么输出"YES",否则输出"NO".

题目分析:

模拟题,扫描一遍字符串s,再定义一个字符数组p[] = {"hello"}。逐个比较s中的字母,当能匹配p[0]后,再查询s能否匹配p[1],一直匹配到最后一个。如果都能匹配,那么就说明可以,否则不行。

参考代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
	char s[110];
	char p[] = {"hello"};
	cin >> s;
	int len = strlen(s);
	int k = 0;
	for(int i=0; i<len; i++){
		if(s[i]==p[k])
			k ++;
	}
	if(k==5)
		cout << "YES";
	else
		cout << "NO";
	return 0;
} 

猜你喜欢

转载自www.cnblogs.com/gdgzliu/p/13377010.html
58A