UVA12897 - Decoding Baby Boos

没必要每次都真的修改一遍字母值,用一个标记表示字母最后的值,最后一遍的时候再进行修改

#include<cstdio>
#include<cstring>

const int maxlen = 1000000+10;

char str[maxlen];


char to[256];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    to['_'] = '_';
    while(T--){
        scanf("%s",str);
        int e;
        scanf("%d\n",&e);
        for(int i = 'A'; i <= 'Z'; i++) to[i] = i;
        for(int j = 0; j < e; j++){
            int v,u;
            char buf[10];

            fgets(buf,9,stdin);
            int i = 0;
            for(;i<9;i++) if('A'<=buf[i]&&buf[i]<='Z'){
                v = buf[i++]; break;
            }

            for(;i<9;i++) if('A'<=buf[i]&&buf[i]<='Z'){
                u = buf[i++] ; break;
            }
            for(int i = 'A'; i <= 'Z';i++){
                if(u == to[i])
                    to[i] = v;
            }
        }
        int len = strlen(str);
        for(int i = 0; i < len; i++){
            str[i] = to[str[i]];
        }
        printf("%s\n",str);
    }
    return 0;
}

 

posted @ 2015-07-16 20:55  陈瑞宇  阅读(334)  评论(0编辑  收藏  举报