Codeforces 591B Rebranding

本文详细解析了一道关于字符串变换的算法题,通过定义两个数组记录字母间的转换关系,实现了一个小公司名称的重新品牌化过程。从输入的公司名称和一系列字母替换规则出发,逐步演示如何高效地得到最终的品牌名称。
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 xi by 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 letters xi and yi.

Output

Print the new name of the corporation.

Examples
Input
6 1
police
p m
Output
molice
Input
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
Output
cdcbcdcfcdc
Note

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

题目链接:http://codeforces.com/problemset/problem/591/B

这道题挺考思维的

详解:-1s1代表的本意abcdefg...0s1数组的下标和初始值0123456...1a-b1023456...2b-c1203456...3a-d3201456...4e-g3201654...5f-a5201634...6b-b5201634...7s1数组里面最终包含的值所对应的字符fcabgde...

 表二:

s2数组代表的本意abcdefg...
s2数组的下标0123456...
s2数组里面存的值2315604...
s2数组里面最终包含的值所对应的字符cdbfgae...

 定义两个int类型数组来保存26个英文字母所表示的标号,可以开辟30个空间,s1[30] , s2[30]; 第一次交换ch1='a'与ch2='b' ,就交换s1数组里s1[ch1-'a']与s1[ch2-'a']值,之后的1-6做一样的操作;

肯定会有疑问,为什么要交换s1[ch1-'a']与s1[ch2-'a']的值?交换过后是代表第-1行所表示的字符转换为第7行所表示的字符吗?

其实不是,如果你认为交换过后就是代表“第-1行所表示的字符转换为第7行所表示的字符”是错误的。

每次交换你可以看做   例如:表三:

-1s1代表的本意abcdefg...
0s1数组的下标和初始值0123456...
1a-b1023456...
 数组的值+'a'转换为相应的字符bacdefg...
 其实交换过后代表字符'a'是由字符'b'转化而来字符'b'是由字符'a'转化而来      
2b-c1203456...
 数组的值+'a'转换为相应的字符bcadefg...
  字符'a'是由字符'b'转化而来字符'b'是由字符'c'转化而来字符'c'是由字符'a'转化而来     

 你可以想想是不是这样的;这只是举了两个例子;

也就是说表四:

-1s1代表的本意abcdefg......
 s1数组的下标和初始值0123456......
7s1数组里面最终包含的值所对应的字符fcabgde......
 s1数组里面最终的值5201634 
              结论                               字符'a'是由字符'f'转化而来字符'b'是由字符'c'转化而来字符'c'是由字符'a'转化而来字符'd'是由字符'b'转化而来字符'e'是由字符'g'转化而来字符'f'是由字符'd'转化而来字符'g'是由字符'e'转化而来......
              推论                               字符'f'最终转化为'a'字符'c'最终转化为'b'字符'a'最终转化为'c'字符'd'最终转化为'b'字符'g'最终转化为'e'字符'd'最终转化为'f'字符'e'最终转化为'g'......

我们现在已经知道字符最终由哪个字符转化而来,也就是说我们得出的是,知道最终的字符,我们能找到它是由哪个最初的字符转换而来的,之后的怎么办呢? 我们需要转化为 题意给出了最初字符,能找到他最终要转换为哪个字符  这个就要好好思考一下怎么转换了!

其实定义两个数组,可以理解为,表四:  做这样的处理: 之后就变为表二: 

 

for(i=0; i<26; i++)
     s2[s1[i]]=i;

 

 为什么要这样处理呢?这样处理过后就会简化很多;再加上下面这个步骤,这道题就算是解决了;

1 for(i=0; i<n; i++)
2 {
3    str[i]=s2[str[i]-'a']+'a';
4 }

附上AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 2000010
 4 int main()
 5 {
 6     char str[N],ch1,ch2;
 7     int s1[30],s2[30];
 8     int n,m,i,x,y,t;
 9     while(scanf("%d%d",&n,&m)!=EOF)
10     {
11         scanf("%s",str);
12         for(i=0;i<26;i++)
13             s1[i]=i;
14         for(i=0;i<m;i++)
15         {
16             scanf(" %c %c", &ch1, &ch2);
17             x=ch1-'a';
18             y=ch2-'a';
19             t=s1[x];
20             s1[x]=s1[y];
21             s1[y]=t;
22         }
23         for(i=0;i<26;i++)
24             s2[s1[i]]=i;
25         for(i=0;i<n;i++)
26         {
27             str[i]=s2[str[i]-'a']+'a';
28         }
29         printf("%s\n",str);
30     }
31     return 0;
32 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值