HDU 1181 变形课

变形课

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  HDU. Original ID: 1181
64-bit integer IO format: %I64d      Java class name: Main
 
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. 
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
 

Input

测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
 

Output

如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
 

Sample Input

so

soon

river

goes

them

got

moon

begin

big

0

Sample Output

Yes.
Hint
Hint
Harry 可以念这个咒语:"big-got-them".

Source

 
解题:有向图传递闭包。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 bool mp[27][27] = {false};

18 char str[1010];

19 void Floyd(){

20     for(int k = 0; k < 27; k++){

21         for(int i = 1; i < 27; i++){

22             for(int j = 1; j < 27; j++)

23                 mp[i][j] |= mp[i][k]&mp[k][j];

24         }

25     }

26 }

27 int main() {

28     while(~scanf("%s",str)){

29         int len = strlen(str);

30         if(len == 1 && str[0] == '0'){

31             Floyd();

32             mp['b'-'a']['m'-'a']?puts("Yes."):puts("No.");

33             memset(mp,false,sizeof(mp));

34         }else{

35             mp[str[0]-'a'][str[len-1]-'a'] = true;

36         }

37     }

38     return 0;

39 }
View Code

 

你可能感兴趣的:(HDU)