CodeForces 591B

题意:给你一个长度为n的字符串,m条规则,规则是将字符串中x变为y,将y变为x,输出m次变换后的字符串。

题解:字符串全是小写字母组成,最多有26中字符,将这26种字符的变换处理出来,就可直接输出走后的答案。

#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;

char z[]="abcdefghijklmnopqrstuvwxyz";

int main()
{
    int n,m;
    char s[200005];
    scanf("%d%d%s",&n,&m,s);
    getchar();
    char c[5];
    while(m--)
    {
        gets(c);
        int pa,pb;
        for(int i=0;i<26;i++)
        {
            if(z[i]==c[0]) pa=i;
            if(z[i]==c[2]) pb=i;
        }
        z[pa]=c[2];
        z[pb]=c[0];
    }
    for(int i=0;i<n;i++)
        printf("%c",z[s[i]-'a']);
    printf("\n");
    return 0;
}

 

posted on 2016-05-25 14:25  猫哥小俊  阅读(167)  评论(0编辑  收藏  举报

导航