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;

}

 

你可能感兴趣的:(uva)