Decoding Baby Boos 只需对26个字母哈希映射

                        Decoding Baby Boos

 

题意,给出一个字符串。给出n个命令,   x,y,  把字符串中所有的y变成x.   求n个命令执行完后的字符串。直接用一个数组hash[i]=j,表示   字母i对应j;

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF=0x4fffffff;
17 const int EXP=1e-5;
18 const int MS=1000005;
19 const int SIZE=26;
20 
21 int ha[SIZE];
22 char str[MS];
23 
24 int main()
25 {
26       int T;
27       scanf("%d",&T);
28       while(T--)
29       {
30             for(int i=0;i<SIZE;i++)
31                   ha[i]=i;
32             scanf("%s",str);
33             int len=strlen(str);
34             int n;
35             scanf("%d",&n);
36             char s1[5],s2[5];
37             while(n--)
38             {
39                   scanf("%s%s",s1,s2);
40                   int u=s2[0]-'A';
41                   int v=s1[0]-'A';
42                   for(int i=0;i<SIZE;i++)
43                         if(ha[i]==u)
44                               ha[i]=v;
45             }
46             for(int i=0;i<len;i++)
47             {
48                   if(str[i]=='_')
49                         printf("%c",str[i]);
50                   else
51                         printf("%c",ha[str[i]-'A']+'A');
52             }
53             printf("\n");
54       }
55       return 0;
56 }

 

posted @ 2015-04-04 19:38  daydaycode  阅读(259)  评论(0编辑  收藏  举报