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 letters xiand yi.
Output
Print the new name of the corporation.
Sample test(s)
input
output
Note
In the second sample the name of the corporation consecutively changes as follows:
一道想法题目:无非就是字符 x 最后映射成了另一个字符 y,操作的也就是a,b,c,d...z这26个字符。初始化ch[i]存放的是第i个字符, pos[i]=i,记录第i个字符的位置。那么 a<->b互换,也就是在ch[]中交换a,b两个字符的位置(通过pos[a-'a'] 和 pos[b-'a']找到),并同时交换pos[]的值。最终ch[i]表示字符i+'a'映射的字符。
C. Median Smoothing
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.
Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bnobtained by the following algorithm:
b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.
The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.
In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.
The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.
If the sequence will never become stable, print a single number - 1.
Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space — the resulting sequence itself.
In the second sample the stabilization occurs in two steps:
, and the sequence 00000 is obviously stable.
构造题目:如果a[i]==a[i+1]那么a[i]和a[i+1]都是stable的数字,通过stable的数字将原串分成多个串Si,形如0101(或1010), 01010(10101) 。
原串最终成为stable的次数,也就是子串Si成为stable变换次数的最大值。那么如何计算子串Si成为stable的次数呢?其实找一下规律就好了!
0101.....10,这样的序列,区间为[l, r], a[l]==a[r]最终变成稳定序列 0000...00。变换的次数为(r-l+1)/2;
0101.....01,这样的序列,区间[l, r], a[l]!=a[r]最终变成稳定序列0000.....1111。0和1各占一半。变换的次数为(r-l+1)/2 -1。