Regionals 2014 >> Asia - Dhaka >> 6917 - Decoding Baby Boos

6917 - Decoding Baby Boos

题目:


Regionals 2014 >> Asia - Dhaka >> 6917 - Decoding Baby Boos_第1张图片

题目大意:修改多次,输入a,b,把此字符串中b字母修改为a

题目思路:直接做会超时,先处理修改的情况。开一个数组fa[]存储修改后的结果,每次修改,遍历26个字母的fa[],如果满足条件则修改fa[]。

题目链接:6917 - Decoding Baby Boos

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int fa[30];

int main(){
    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int r;
        cin >> r;
        for (int i = 0; i < 26; i++)
        {
            fa[i] = i;
        }
        while(r--)
        {
            char a,b;
            cin >> a >> b;  
            for(int i = 0; i < 26; i++)
            {
                if (fa[i] == (b - 'A')) fa[i] = a - 'A';
            }
        }
        for (int i = 0; i < s.size(); i++)
        {
            if (isupper(s[i]))
                s[i] = fa[s[i] - 'A'] + 'A';            
        }
        cout << s << endl;
    } 
    return 0;
}

你可能感兴趣的:(Regionals,Asia---Dha,6917)