CodeForces-58A-Chat room

A. Chat room
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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".

Sample test(s)
input
ahhellllloou
output
YES
input
hlelo
output
NO

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string x = "hello";
	string y;
	cin >> y;
	bool isTrue = true;
	int length = y.length();
	for (int i = 0; i < 5; i++){
		if (y.find(x[i])!=string::npos){
			y=y.substr(y.find(x[i])+1, length);
		}
		else{
			isTrue = false;
			break;
		}
	}
	cout << (isTrue ? "YES" : "NO");

	return 0;
}
改进思路:这样写太过麻烦,应该采用长字符串在外循环的方法,没有运用到贪心算法,这样写因为采用hello在外循环,则里面需要对输入的字符串进行切割操作、改进后结果如下:
<pre name="code" class="cpp">#include<iostream>
#include<string>

using namespace std;

int main()
{
	char x[] = { 'h', 'e', 'l', 'l', 'o' };
	string y;
	int num = 0;
	cin >> y;
	for (int i = 0; i < y.length(); i++){
		if (y[i] == x[num]){
			num++;
		}
	}
	cout << (num == 5 ? "YES" : "NO");
	system("pause");
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值