变形课
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 20146 Accepted Submission(s): 7247
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
so soon river goes them got moon begin big 0
Yes.Harry 可以念这个咒语:"big-got-them".HintHint
a开头b结尾, A可以变成B,题目要求有没有b开头,m结尾的单词,而单词可以通过首尾相连“变长”;
"big-got-them".
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
string a;
string str[1000];
int k = 0, flag = 0;
int vis[1000];
int dfs(int x)
{
vis[x] = 1;
int len = str[x].length();
if(str[x][len - 1]=='m')
return 1;
for(int i = 0; i < k; i++)
{
if(vis[i])
continue;
if(str[x][len-1] == str[i][0]&&!vis[i])
{
if(dfs(i))
return 1;
vis[i] = 0;
}
}
return 0;
}
int main()
{
memset(vis,0,sizeof(vis));
k = 0;
while(cin >> a)
{
if(a=="0")
{
for(int i = 0; i < k; i++)
{
if(str[i][0]=='b')
{
if(dfs(i))
{
flag = 1;
cout << "Yes." << endl;
break;
}
}
}
if(flag == 0)
cout << "No." << endl;
k = 0, flag = 0;
memset(vis,0,sizeof(vis));
}
else
{
str[k++] = a;
}
}
return 0;
}