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
  1. 6 1
    police
    p m
output
  1. molice
input
  1. 11 6
    abacabadaba
    a b
    b c
    a d
    e g
    f a
    b b
output
  1. cdcbcdcfcdc
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'映射的字符。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #define N 200005
  5. using namespace std;
  6. char str[N];
  7. int pos[], ch[];
  8.  
  9. int main(){
  10. int n, m;
  11. scanf("%d%d%s", &n, &m, str);
  12. getchar();
  13. for(int i=; i<; ++i)
  14. pos[i] = ch[i] = i;
  15. char s[];
  16. while(m--){
  17. gets(s);
  18. if(s[] == s[]) continue;
  19. swap(ch[pos[s[]-'a']], ch[pos[s[]-'a']]);
  20. swap(pos[s[]-'a'], pos[s[]-'a']);
  21. }
  22. for(int i=; i<n; ++i)
  23. printf("%c", ch[str[i]-'a']+'a');
  24. printf("\n");
  25. return ;
  26. }
                                   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.

Input

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.

Output

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.

Sample test(s)
input
  1. 4
    0 0 1 1
output
  1. 0
    0 0 1 1
input
  1. 5
    0 1 0 1 0
output
  1. 2
    0 0 0 0 0
Note

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。

  1. #include<iostream>
  2. #include<cstdio>
  3. #define N 500005
  4. using namespace std;
  5. int a[N];
  6. int main(){
  7. int n;
  8. scanf("%d", &n);
  9. for(int i=; i<=n; ++i)
  10. scanf("%d", &a[i]);
  11. int ans = ;
  12. for(int i=; i<=n;){
  13. int j = i;
  14. while(j+<=n && a[j]!=a[j+])
  15. ++j;
  16. //[i, j]这段区间需要作调整, 其中第i个位置和第j个位置的数字已经是stable
  17. int len = j-i+;
  18. if(a[i] == a[j]) {//形如01010
  19. for(int k=i+; k<=j; ++k)
  20. a[k] = a[i];
  21. ans = max(ans, len/);
  22. } else {//形如 010101
  23. int k;
  24. for(k=i+; k<=(i+j)/; ++k)
  25. a[k] = a[i];
  26. for(k; k<j; ++k)
  27. a[k] = a[j];
  28. ans = max(ans, len/-);
  29. }
  30. i = j+;
  31. }
  32. printf("%d\n", ans);
  33. for(int i=; i<=n; ++i){
  34. if(i!=) printf(" ");
  35. printf("%d", a[i]);
  36. }
  37. printf("\n");
  38. return ;
  39. }

Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing的更多相关文章

  1. Codeforces Round #327 (Div. 2) B. Rebranding 水题

    B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...

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

    B. Rebranding   The name of one small but proud corporation consists of n lowercase English letters. ...

  3. Codeforces Round #327 (Div. 2) B Rebranding(映射)

    O(1)变换映射,最后一次性替换. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; char s[ ...

  4. Codeforces Round #327 (Div. 2)

    题目传送门 水 A - Wizards' Duel 题目都没看清就写了,1e-4精度WA了一次... /************************************************ ...

  5. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

  6. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

  7. Codeforces Round #327 (Div. 2)B(逻辑)

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Codeforces Round #327 (Div. 2) E. Three States BFS

    E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...

  9. Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理

    D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...

随机推荐

  1. 【BZOJ】3997: [TJOI2015]组合数学

    题意 \(N \times M\)的网格,一开始在\((1, 1)\)每次可以向下和向右走,每经过一个有数字的点最多能将数字减1,最终走到\((N, M)\).问至少要走多少次才能将数字全部变为\(0 ...

  2. Notepad++ 默认快捷键

    Notepad++绝对是windows下进行程序编辑的神器之一,要更快速的使用以媲美VIM,必须灵活掌握它的快捷键,下面对notepad++默认的快捷键做个整理(其中有颜色的为常用招数):     1 ...

  3. swift-func(函数)

    函数是一个组织在一起语句集合,以执行特定任务. Swift 函数类似于简单 C 函数以及复杂的 Objective C 语言函数. 它使我们能够通过函数调用内部的局部和全局参数值. 像其他任何语言一样 ...

  4. xpath tutorial

    http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html http://www.w3schools.com/xpath/defaul ...

  5. 前端UI框架和JS类库

    一.前端框架库: 1.Zepto.js 地址:http://www.css88.com/doc/zeptojs/ 描述:Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...

  6. S3C2440UART之FIFO

    一.基础知识 S3C2440有3个独立的串口,每一个都可以利用DMA和中断方式操作.每个包含2个64字节FIFO,一个收,一个发.非FIFO模式相当于FIFO模式的一个寄存器缓冲模式.每一个UART有 ...

  7. script async 和script defer的区别

    浏览器对js文件的操作主要有两部分:下载和执行: js文件下载在有些浏览器中是并行的,在有些浏览器中是串行的,如:IE8.firefox3.chrome2都是串行下载的: 执行在所有浏览器中默认是阻塞 ...

  8. FreeMarker中文API手册(完整)

    FreeMarker概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用 ...

  9. Javaee中文乱码解决方法

    分类: javaee2015-07-09 16:35 29人阅读 评论(0) 收藏 编辑 删除 post 中文乱码解决方式 接受数据的时候设置 request.setCharacterEncoding ...

  10. LVS DR模式 RealServer 为 Windows 2008 R2配置

    有3篇文档详细介绍 http://kb.linuxvirtualserver.org/wiki/Windows_Servers_in_LVS/DR_and_LVS/TUN_Clusters http: ...