Codeforces 591B Rebranding

简介: B. Rebranding time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output ...
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数组代表的本意 a b c d e f g ...
s2数组的下标 0 1 2 3 4 5 6 ...
s2数组里面存的值 2 3 1 5 6 0 4 ...
s2数组里面最终包含的值所对应的字符 c d b f g a e ...

 定义两个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行所表示的字符”是错误的。

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

-1 s1代表的本意 a b c d e f g ...
0 s1数组的下标和初始值 0 1 2 3 4 5 6 ...
1 a-b 1 0 2 3 4 5 6 ...
  数组的值+'a'转换为相应的字符 b a c d e f g ...
  其实交换过后代表 字符'a'是由字符'b'转化而来 字符'b'是由字符'a'转化而来            
2 b-c 1 2 0 3 4 5 6 ...
  数组的值+'a'转换为相应的字符 b c a d e f g ...
    字符'a'是由字符'b'转化而来 字符'b'是由字符'c'转化而来 字符'c'是由字符'a'转化而来          

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

也就是说表四:

-1 s1代表的本意 a b c d e f g ......
  s1数组的下标和初始值 0 1 2 3 4 5 6 ......
7 s1数组里面最终包含的值所对应的字符 f c a b g d e ......
  s1数组里面最终的值 5 2 0 1 6 3 4  
               结论                                字符'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 }

 

目录
相关文章
|
6月前
codeforces 322 B Ciel and Flowers
有红绿蓝三种颜色的画,每种拿三朵可以组成一束花,或者各拿一朵组成花束,告诉你每种花的数目,求出可能组成最多的花束。 如果你的代码过不了,考虑一下 8 8 9这种组合。 因为数据量很大,我的思想就是局部和总体采用不同的策略。
26 0
|
6月前
codeforces 327 A Ciel and Dancing
给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1。 这里要注意很多细节 比如全为1,要求必须翻转,这时候我们只要翻转一个1就可以了,对于其他情况,我们只要计算区间里面如果0多于1,将其翻转后计算1的总数,然后取最大值。
20 0
|
6月前
|
C++
codeforces 305 C. Ivan and Powers of Two
我的思路是这样的,由2^a+2^a = 2^(a+1)可知,如果有两个连续的数a,我们可以把他们合并为a+1放入集合中,使集合中没有重复的数,我可以用stl里的set。如果想要满足题目中的要求,集合中必须有最大那个数个元素,缺多少就可以计算出来了。
15 0
|
6月前
codeforces 322 A Ciel and Dancing
有n个男孩和m个女孩,他们要结对跳舞,每对要有一个女孩和一个男孩,而且其中一个要求之前没有和其他人结对,求出最大可以结多少对。
21 0
|
6月前
codeforces 304A. Pythagorean Theorem II
给你一个n,计算出1 ≤ a ≤ b ≤ c ≤ n.使得由abc构成的三角形满足勾股定理,c为斜边。 没有简单的方法,直接爆力,但是要注意,有些abc满足勾股定理的表达式,但不一定是三角形,所以要判断一下,根据三角形三边的性质,两边之和大于第三边,两边之差小于第三边。
31 0
C - Rumor CodeForces - 893C
C - Rumor CodeForces - 893C
62 0
|
人工智能
Codeforces 777C Alyona and Spreadsheet
C. Alyona and Spreadsheet time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard ...
1133 0
|
人工智能
Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:standard input output:stan...
862 0