UVA 12897 decoding Baby Boos

Source: Internet
Author: User

Osantu have been tagged as the best Bangladeshi contestant of present time by many and now he ismostly busy with the Sweete St problem's life-a baby boy. But Osantu often struggles to decodethe, the baby makes throughout the year and so he asks for your help. He has converted Thesound, the baby makes into a huge string. He thinks that made by the baby was often hard tounderstand because he replaces one character with another character. So's diary he has Prepareda list of replacement rules. Your job is to reverse these replacements and find the actual text this thebaby wanted to say although in many cases the R Ules is not reversible.

Input

First line of the input file contains a positive integer T (t≤6) which denotes what many test cases arethere in the input File. The description of each test case was given Below:first line of each test case contains a non-empty string S (containing on Ly uppercase characters Andunderscore). The length of this string can is up to 1000000. Next line contains a positive an integer R (r≤10000), which denotes how many character replacement sequences follow. Each of the next rlines contains the characters AI and bi (both AI and bi is uppercase letters and separated by a singles PACE) which denotes that and pronouncing the baby replaces character Ai with character Bi. As Thisreplacement list is prepared by Osantu (who have short term memory) so the list can contain the samereplacement rule s twice, there can be cyclic the rules like ' A ' are replaced with ' B ', ' B ' are replaced with ' C ' and ' C ' are replaced with ' a ' and Also there can be contradicting the rules like ' a ' are replaced with ' B ' and ' A ' is replaced With ' C ' etc. So-what are simply need to do are apply the reverse of those rules in the Orderthey appear in the input although it could not Seem logical.


Output


For each set of input produce one line of output. This line contains the string, 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 the rule all ' B ' is Replacedwith ' A '. So the string becomes "Aaaaccy". The 2nd rule says ' B ' is replaced with ' C ' and so-reversethis rule we replace all ' C ' s with ' B ' and so the string becom Es "Aaaabby". The 3rd rule says that's ' C ' isreplace with ' a ' and so to reverse that we are replace all ' A's with ' C ' and so the string fi Nally 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

Topic: Give a string and then give you a replacement of the topic corresponding table.

Idea: Make an alphabet replace many times of the letters once to get an array inside the store.

AC Code

#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;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.