UVA 12897 Decoding Baby Boos

来源:互联网 发布:未来软件价格 编辑:程序博客网 时间:2024/05/01 10:31

   Osantu has been tagged as the best Bangladeshi contestant of present time by many and now he ismostly busy with the sweetest problem of his life — a baby boy. But Osantu often struggles to decodethe sound that the baby makes throughout the year and so he asks for your help. He has converted thesound that the baby makes into a huge string. He thinks that sound made by the baby is often hard tounderstand because he replaces one character with another character. So in his diary he has prepareda list of replacement rules. Your job is to reverse these replacements and find the actual text that thebaby wanted to say although in many cases the rules are not reversible.

 Input

First line of the input file contains a positive integer T (T ≤ 6) which denotes how many test cases arethere in the input file. The description of each test case is given below:First line of each test case contains a non-empty string S (containing only uppercase characters andunderscore). The length of this string can be up to 1000000. Next line contains a positive integer R(R ≤ 10000), which denotes how many character replacement sequences follow. Each of the next Rlines contains two characters ai and bi (both ai and bi are uppercase letters and separated by a singlespace) which denotes that while pronouncing the baby replaces character ai with character bi. As thisreplacement list is prepared by Osantu (who has short term memory) so the list can contain the samereplacement rules twice, there can be cyclic rules like ‘A’ is replaced with ‘B’, ‘B’ is replaced with ‘C’and ‘C’ is replaced with ‘A’ and also there can be contradicting rules like ‘A’ is replaced with ‘B’ and ‘A’is replaced with ‘C’ etc. So what you simply need to do is apply the reverse of those rules in the orderthey appear in the input although it may not seem logical.


Output


For each set of input produce one line of output. This line contains the string that is found by applyingall the R replacement rules.Illustration of the 2nd sample input:First replacement rule says the baby replaces ‘A’ with ‘B’. So to reverse that rule all ‘B’ s are replacedwith ‘A’. So the string becomes “AAAACCY”. The 2nd rule says ‘B’ is replaced with ‘C’ and so to reversethis rule we replace all ‘C’s with ‘B’ and so the string becomes “AAAABBY”. The 3rd rule says that ‘C’ isreplace with ‘A’ and so to reverse that we now replace all ‘A’s with ‘C’ and so the string finally becomes“CCCCBBY”.


Sample Input

2AVVU_TUMI_COLING_PARO_NAY

3

B  V 

D L

H Y

ABBCCY

3

A B

B C

C A

Sample Output

ABBU_TUMI_CODING_PARO_NAH

CCCCBBY

题目大意:给一个字符串然后给你一个替换的题目对应表。

思路:弄一个字母表把替换很多次的字母一次替换掉弄到一个数组里面存储。

ac代码

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespace std;char a,b;int f[30];int main(){    int t;    cin>>t;    while(t--)    {        string s;        cin>>s;        int r;        cin>>r;        for(int i=0;i<30;i++)        {            f[i]=i;        }        for(int i=0;i<r;i++)        {            cin>>a>>b;            for(int j=0;j<30;j++)            {                 if(f[j]==b-'A') f[j]=a-'A';            }        }        for(int i=0;i<s.size();i++)        {            if(s[i]<'A'||s[i]>'Z') cout<<s[i];            else printf("%c",f[s[i]-'A']+'A');        }        cout<<endl;    }    return 0;}



0 0
原创粉丝点击