深搜
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Edge{
int to, next;
};
Edge edge[10000];
int head[30], tot;
bool mark[30];
int add_edge( int a, int b ){
edge[tot].to = b;
edge[tot].next = head[a];
head[a] = tot++;
return 0;
}
bool DFS( int pos ){
if( pos == 'm' - 'a' ){
return true;
}
mark[pos] = true;
for( int i = head[pos]; i != -1; i = edge[i].next ){
int to = edge[i].to;
if( !mark[to] ){
if( DFS( to ) ){
return true;
}
}
}
return false;
}
int main(){
char s[100];
int length;
while( scanf( "%s", s ) != EOF ){
tot = 0;
memset( head, -1, sizeof( head ) );
length = strlen( s );
add_edge( s[0] - 'a', s[length-1] - 'a' );
while( scanf( "%s", s ) && strcmp( s, "0" ) ){
length = strlen( s );
add_edge( s[0] - 'a', s[length-1] - 'a' );
}
memset( mark, false, sizeof( mark ) );
mark['b'-'a'] = true;
if( DFS( 'b' - 'a' ) ){
cout << "Yes." << endl;
}else{
cout << "No." << endl;
}
}
return 0;
}