一、题目描述:
大家都很关心考试难易程度。K老师出题有一个规律,在出题之前,他会随机写下一个字符
串,只要这个字符串中能按顺序找到E,A,S,Y四个字母。他出题就会比较简单。你拿到了这个
字符串,请你告诉考试的题目难不难吧。
二、输入格式:
输入数据有多组,每组占一行,由一个字符串组成(字符串的长度不超过1000)
三、输出格式:
对于每组输入,输出一行,对应一个要求的答案(题目容易就输出easy,难就输出difficult)。
四、输入样例:
eAsy
SEoAtSNY
五:输出样例:
difficult
easy
六、解决方法:
(1)思路:
- 使用双指针来匹配两个字符串,当 i<1000 且 j=4时,才匹配,其他情况均不匹配
(2)代码实现:
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
//自定义函数,是否匹配了 E、A、S、Y
bool match(string s){
int i=0,j=0; //双指针,分别指向 EASY和待匹配的数组
string e="EASY";
while(i<1000 && j<4){
if(s[i] == e[j]){
i++;
j++;
}
else i++;
}
if(i<1000 && j==4) return true;
else return false;
}
int main()
{
string s;
while(cin>>s){
if(match(s))
cout<<"easy"<<endl;
else
cout<<"difficult"<<endl;
}
system("pause");
}