hdu1181 dfs 字符串首尾可拼接,问是否可寻找到一条字串路径使得首尾分别是‘b’和‘m’,简单的搜索+回溯

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define prime1 1e9+7
 8 #define prime2 1e9+9
 9 #define scand(x) scanf("%llf",&x)
10 #define f(i,a,b) for(int i=a;i<=b;i++)
11 #define scan(a) scanf("%d",&a)
12 #define dbg(args) cout<<#args<<":"<<args<<endl;
13 #define pb(i) push_back(i)
14 #define ppb(x) pop_back(x)
15 #define maxn 100005
16 int m,t,tot;
17 struct node{
18     char st,ed;
19 }n[maxn];
20 char s[maxn];
21 bool vis[maxn];
22 bool dfs(char a)
23 {
24     if(a=='m')return true;
25     f(i,0,tot-1)
26     {
27         if(n[i].st==a&&!vis[i])
28         {
29             vis[i]=true;
30             if(dfs(n[i].ed))return true;
31             vis[i]=false;
32         }
33     }
34     return false;
35 }
36 int main()
37 {
38     std::ios::sync_with_stdio(false);
39     while(scanf("%s",s)==1)
40     {
41         if(strcmp(s,"0")==0)
42         {
43             if(dfs('b'))
44             {
45                 pf("Yes.\n");
46             }
47             else pf("No.\n");
48             tot=0;
49             memset(vis,false,sizeof(vis));
50             continue;
51         }
52         n[tot].st=s[0];
53         n[tot].ed=s[strlen(s)-1];
54         tot++;
55     }
56  } 

猜你喜欢

转载自www.cnblogs.com/randy-lo/p/12384712.html
今日推荐