解题思路
根据题目意思,2次从头遍历字符串,对字符串进行匹配“XiaoQiao”和“XiaoHuiHui”。如果两次都匹配成功,输出Happy,否则输出emm。使用计算器cnt,初始化为0,for(int i=0;i<s.length();++i) 通过i遍历s字符串,如果s[i]==待匹配串的cnt位,++cnt,如果cnt==待匹配串长度,返回true。
Code
#include <stdio.h>
#include <string.h>
bool judge(const char* a, const char* b)
{
int len = strlen(a), lenb = strlen(b);
int j = 0;
for (int i = 0; i < len; ++i) {
if (a[i] == b[j]) ++j;
if (j == lenb) break;
}
return j == lenb;
}
const int N = 1e3 + 5;
int main() {
char str[N];
scanf("%s", str);
if (judge(str, "XiaoQiao") && judge(str, "XiaoHuiHui"))
puts("Happy");
else
puts("emm");
return 0;
}
京公网安备 11010502036488号