http://acm.hdu.edu.cn/showproblem.php?pid=1181
/*
2011-9-10
author:BearFly1990
*/
package acm.hdu.tests;
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class HDU_1181 {
private static boolean canFinish;
private static boolean[] hash = new boolean[101];
private static Word[] words= new Word[101];
private static int index = 0;
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
while(in.hasNext()){
String str = in.next();
if(str.equals("0")){
DFS('b');
System.out.println(canFinish?"Yes.":"No.");
Arrays.fill(hash, false);
canFinish = false;
index = 0;
continue;
}else{
words[index++] = new Word(str.charAt(0),str.charAt(str.length()-1));
}
}
}
public static boolean DFS(char ch){
if(canFinish)return true;
if(ch == 'm'){
canFinish = true;
return true;
}
for(int i = 0; i < index; i++){
if(ch == words[i].begin && !hash[i]){
hash[i] = true;
DFS(words[i].end);
hash[i] = false;
}
}
return false;
}
static class Word{
char begin;
char end;
public Word(char begin,char end){
this.begin = begin;
this.end = end;
}
}
}