问题描述
问题分析
通过DFS来递归查找有无以’m’结尾的单词
递归开始条件设为’b’
每处理一组数据进行初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| #include<stdio.h>
#include<string.h>
char s[1200][1200];
int flag[1200];
int cnt=0;
bool Can=false;
void judge(char c) {
if(c=='m')
Can=true;
bool win=false;
for(int i=0; i<cnt; i++) {
if(s[i][0]==c&&flag[i]==0) {
flag[i]=1;
int l=strlen(s[i]);
judge(s[i][l-1]);
}
}
}
int main() {
while(scanf("%s",s[cnt++])!=EOF) {
if(s[cnt-1][0]=='0') {
judge('b');
printf("%s\n",Can?"Yes.":"No.");
memset(flag,0,sizeof(flag));
cnt=0;
Can=false;
}
}
return 0;
}
|