思维训练 之 hdu 4545

//  [5/4/2014 Sjm]
/*
遍历,匹配即可。
注意:一个字符可以转换成多个字符。
*/
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 using namespace std;
 9 string str1, str2;
10 vector<char> myReplace[26];
11 
12 void Solve()
13 {
14     int len1 = str1.size(), len2 = str2.size();
15     int tlen = 0;
16     for (int i = 0; i < len2; i++) {
17         if (str2[i] == str1[tlen]) tlen++;
18         else {
19             for (int j = myReplace[str2[i] - 'a'].size() - 1; j >= 0; j--) {
20                 if (str1[tlen] == myReplace[str2[i] - 'a'][j]) {
21                     tlen++;
22                     break;
23                 }
24             }
25             
26         }
27         if (tlen == len1) {
28             printf("happy
");
29             return;
30         }
31     }
32     printf("unhappy
");
33 }
34 
35 int main()
36 {
37     //freopen("input.txt", "r", stdin);
38     //freopen("output.txt", "w", stdout);
39     myReplace[2].push_back('#');
40     int T;
41     scanf("%d", &T);
42     for (int t = 1; t <= T; t++)
43     {
44         memset(myReplace, 0, sizeof(myReplace));
45         printf("Case #%d: ", t);
46         cin >> str1 >> str2;
47         int n;
48         scanf("%d", &n);
49         while (n--) {
50             char c1, c2;
51             getchar();
52             scanf("%c %c", &c1, &c2);
53             //cin >> c1 >> c2;
54             myReplace[c1 - 'a'].push_back(c2);
55         }
56         Solve();
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/shijianming/p/4140853.html