题目描述
这个世界上有着一种奇怪的生物叫做嘤怪,经常嘤,Hao同学作为一个励志要做打到嘤怪的男人,知己知彼方能百战不殆,Hao同学发现嘤怪时常会散发一种奇怪的脑电波QAQ,但是脑电波中经常会夹杂着其它一些无用的情绪,Hao同学为了研究嘤怪发现需要提取嘤怪脑电波中QAQ出现的频率,因此Hao Jia Qi同学需要写一个程序来提取脑电波中的QAQ数量。
输入格式
输入一个字符串,其长度大于1且不超过105
输出格式
输出QAQ出现的不重叠次数,如果次数为0,则输出yes
输入样例
abc QAQ ddQAQAQ
Helloabc2019
输出样例
2
yes
数据范围与提示
QAQAQ只算出现一次,不算2次。
#include <stdio.h>
#define N 105
int main()
{
char a[N];
const char *s = "QAQ";
fgets(a, N, stdin);
const char *p = a;
int count = 0;
while (*p)
{
const char *q = s;
if (*p == *q)
{
while (*p && *q && *p == *q)
{
p++;
q++;
}
if (*q == '\0')
count++;
}
else
{
p++;
}
}
if (count > 0)
printf("%d\n", count);
else
printf("yes\n");
return 0;
}
#include<iostream>
#include<string>
int main()
{
std::string str;
std::getline(std::cin, str);
unsigned short count = 0;
size_t i = 0;
while (i != str.npos)
{
i = str.find("QAQ", i + 3);
count++;
}
if (--count)
std::cout << count;
else
std::cout << "yes";
return 0;
}
可以用std::string::find函数来查找字符串