zoukankan      html  css  js  c++  java
  • 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.
    ");
    46             }
    47             else pf("No.
    ");
    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  } 
  • 相关阅读:
    Python数据结构与算法(几种排序)
    jquery元素节点操作
    Jquery事件委托
    Jquery事件冒泡
    jquery事件
    尺寸相关、滚动事件
    jquery属性操作
    jquery选择器
    JavaScript面向对象
    jQuery powerFloat万能浮动层下拉层插件
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12384712.html
Copyright © 2011-2022 走看看