变形课

题目链接

题意

让你连起来字符串,看看最终的字符串能否是第一个字母为b,最后一个字符是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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
int vis[1001];
int tot;
string s[1001];
int bfs()
{
queue<int>Q;
for(int i = 0 ;i < tot; ++i)
{
if(s[i][0] == 'b')
{
Q.push(i);
vis[i] = 1;
}
}
while(!Q.empty())
{
int A = Q.front();
Q.pop();
int sz = s[A].size();
char c = s[A][sz - 1];
if(c == 'm')
return 1;
for(int i = 0; i < tot; ++i)
{
if(s[i][0] == c && !vis[i])
{
vis[i] = 1;
Q.push(i);
}
}
}
return 0;
}
int main()
{
while(cin >> s[0]){
tot = 1;
while(cin >> s[tot])
{
if(s[tot][0] == '0')
break;
tot++;
}
memset(vis,0,sizeof(vis));
int c = bfs();
if(c == 1)
cout << "Yes." << endl;
else
cout << "No." << endl;
}
}