hdu–1181–DFS(基礎之一)

來源:互聯網
上載者:User
變形課

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 10099    Accepted Submission(s): 3765


Problem Description呃......變形課上Harry碰到了一點小麻煩,因為他並不像Hermione那樣能夠記住所有的咒語而隨意的將一個棒球變成刺蝟什麼的,但是他發現了變形咒語的一個統一規律:如果咒語是以a開頭b結尾的一個單詞,那麼它的作用就恰好是使A物體變成B物體. 
Harry已經將他所會的所有咒語都列成了一個表,他想讓你幫忙計算一下他是否能完成老師的作業,將一個B(ball)變成一個M(Mouse),你知道,如果他自己不能完成的話,他就只好向Hermione請教,並且被迫聽一大堆好好學習的道理. 


Input測試資料有多組。每組有多行,每行一個單詞,僅包括小寫字母,是Harry所會的所有咒語.數字0表示一組輸入結束. 


Output如果Harry可以完成他的作業,就輸出"Yes.",否則就輸出"No."(不要忽略了句號) 


Sample Input

sosoonrivergoesthemgotmoonbeginbig0
 


Sample Output

Yes.  
#include<cstdio>#include<cstring>//#include<cstdlib>#include<iostream>using namespace std;  const int maxn=100000;    char start[maxn],end[maxn];  int vis[maxn],t;  bool ok;    void DFS(int x){      if(end[x]=='m'){          ok=true; return;      }      for(int i=0;i<t;++i){          if(vis[i]||start[i]!=end[x])continue;          vis[i]=1;          DFS(i);          vis[i]=0;          if(ok)return;      }  }    int main() {      char s[10000];      while(gets(s)){          t=0;          while(s[0]!='0'){              start[t]=s[0];              end[t++]=s[strlen(s)-1];               gets(s);        }          memset(vis,0,sizeof(vis));          ok=false;          for(int i=0;i<t;++i){              if(start[i]!='b')continue;              vis[i]=1;              DFS(i);          }          if(ok)printf("Yes.\n");          else printf("No.\n");      }      return 0;  }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.