Codeforces Round #327 (Div. 2) B. Rebranding 模拟

B. Rebranding
 

The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.

For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xiby yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation.

Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.

Satisfy Arkady's curiosity and tell him the final version of the name.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively.

The second line consists of n lowercase English letters and represents the original name of the corporation.

Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English lettersxi and yi.

Output

Print the new name of the corporation.

Sample test(s)
input
6 1
police
p m
output
molice
 
题意:给你一个字符串,给你m个操作,每个操作就是将两个规定字母等效变换
题解:模拟就是了
///1085422276
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 100000007
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
#define maxn 200000+5
char a[maxn],A[maxn],B[maxn];
int b[maxn];
int main()
{
    int n=read(),m=read();
    scanf("%s",a);
    for(int i=0;i<26;i++){
        b[i]=i;
    }
    for(int i=1;i<=m;i++){
        scanf("%s%s",A,B);
        if(A[0]==B[0])continue;
        for(int j=0;j<26;j++){
            if(B[0]==b[j]+'a'){
                b[j]=A[0]-'a';
            } else  if(A[0]==b[j]+'a'){
          b[j]=B[0]-'a';
            }
        }
    }
    for(int i=0;i<n;i++){
        printf("%c",b[a[i]-'a']+'a');
    }
    cout<<endl;
    return 0;
}
代码
原文地址:https://www.cnblogs.com/zxhl/p/4911935.html