#include <string.h> // strstr 函数
#include <stdio.h>
#include <stdbool.h> // 可选
bool find(char* src) {
char *p1 = src;
char *p2 = src;
while (p1 && (p1 = strstr(p1, "ab"))) {
p2 = src;
while (p2 && (p2 = strstr(p2, "ba"))) {
if ((p1 + 1 != p2) && (p2 + 1 != p1)) {
return true;
}
p2++;
}
p1++;
}
return false;
}
int main()
{
char sentence[128];
while (fgets(sentence, sizeof(sentence), stdin) != NULL) {
if (find(sentence))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
运行结果,windows 用 Ctrl + Z 结束输入:
a
No
b
No
ab
No
ba
No
aba
No
bab
No
abab
No
baba
No
abba
Yes
baab
Yes
abdfghba
Yes
^Z
请按任意键继续. . .