Codeforces Round #327 (Div. 2) B Rebranding(映射)

O(1)变换映射,最后一次性替换。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int LEN = 2e5+5;
char s[LEN];
char mp[26];
int pos[26];
//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    for(int i = 0; i < 26; i++) pos[i] = i;
    int n, m; scanf("%d%d%s",&n,&m,s);
    while(m--){
        char x[2], y[2]; scanf("%s %s", x, y);
        swap(pos[*x-'a'], pos[*y-'a']);
    }
    for(int i = 0; i < 26; i++) mp[pos[i]] = i+'a';
    for(int i = 0; i < n; i++) putchar(mp[s[i]-'a']);
    return 0;
}
原文地址:https://www.cnblogs.com/jerryRey/p/4909922.html