1181 变形课
文章目录
题意:
思路:
把题目抽象成图,抽象方法如下:
起点:b
终点:m
路径:对于一个字符串 s o o n soon soon,表示从 s s s到 n n n存在一条路径。
根据如上规则建图bfs即可。
AC代码:
#include<bits/stdc++.h>
typedef long long ll;
const int N = 30,M = 2e4+10,INF = 0x3f3f3f3f,mod = 1e9+7;
int que[N];
bool g[N][N];
bool book[N];
bool bfs()
{
memset(book,false,sizeof book);
int head = 0,tail = -1;
que[++tail] = 'b' - 'a';
book['b' - 'a'] = true;
while(tail>=head)
{
int temp = que[head++];
if(temp=='m'-'a')return true;
for(int i = 0 ; i < 26 ; i++)
{
if(!book[i] && g[temp][i])
{
que[++tail] = i;
book[i] = true;
}
}
}
return false;
}
int main()
{
// std::ios::sync_with_stdio(false);
// std::cin.tie(nullptr);
// std::cout.tie(nullptr);
std::string s;
while(std::cin>>s)
{
if(s[0]=='0') std::cout<<(bfs() ? "Yes.\n" : "No.\n"),memset(g,false,sizeof g);
else g[s[0]-'a'][s[s.size()-1]-'a'] = true;
}
return 0;
}