【Codeforces Round #327 (Div. 2) B】 Rebranding 字符变换 同类型打标记

来源:互联网 发布:php导入excel 编辑:程序博客网 时间:2024/06/03 11:07
B. Rebranding
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 1policep m
output
molice
input
11 6abacabadabaa bb ca de gf ab b
output
cdcbcdcfcdc
Note

In the second sample the name of the corporation consecutively changes as follows:


#include<stdio.h>#include<string.h>#include<ctype.h>#include<math.h>#include<iostream>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}const int N=2e5+10,M=0,Z=1e9+7,ms63=1061109567;int n,m;char s[N];char a[128],x,y;//a[i]表示一开始是i的字符最后变成了什么int main(){while(~scanf("%d%d",&n,&m)){for(int i='a';i<='z';i++)a[i]=i;scanf("%s",s);for(int i=1;i<=m;i++){scanf(" %c %c",&x,&y);for(int i='a';i<='z';i++){if(a[i]==x)a[i]=y;else if(a[i]==y)a[i]=x;}}for(int i=0;s[i];i++)s[i]=a[s[i]];puts(s);}return 0;}/*【题意】给你一个长度为n(2e5)的串,串中只包含小写字母。我们先后进行了m(2e5)次替代,对于每次替代规则(x,y),把当前为x的字符变成字符y,并把当前为y的字符变成字符x。问你经过这m次替代之后,串变成了什么模样并输出。【类型】暴力【分析】显然不能模拟。首先观察到,一开始是某个char类型的,最后都会变成相同的char类型。于是我们需要用a[x]表示初始字符是x,最终变成的字符。这样,对于每个替代规则(x,y),我们扫描一下a['a'~'z'],把a[i]==x的变为a[i]=y,把a[i]==y的变为a[i]=x。就可以AC啦。字符集果然是简化复杂度的一大重要突破口。【时间复杂度&&优化】O(26m)【数据】Input6 1policep mOutputmoliceInput11 6abacabadabaa bb ca de gf ab bOutputcdcbcdcfcdc*/

1 0
原创粉丝点击