Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
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.
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.
Print the new name of the corporation.
6 1
police
p m
molice
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
cdcbcdcfcdc
In the second sample the name of the corporation consecutively changes as follows:






#include<iostream>
#include<cstdio>
#include<map>
#define N 200005
using namespace std;
char str[N];
int pos[], ch[]; int main(){
int n, m;
scanf("%d%d%s", &n, &m, str);
getchar();
for(int i=; i<; ++i)
pos[i] = ch[i] = i;
char s[];
while(m--){
gets(s);
if(s[] == s[]) continue;
swap(ch[pos[s[]-'a']], ch[pos[s[]-'a']]);
swap(pos[s[]-'a'], pos[s[]-'a']);
}
for(int i=; i<n; ++i)
printf("%c", ch[str[i]-'a']+'a');
printf("\n");
return ;
}
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.
4
0 0 1 1
0
0 0 1 1
5
0 1 0 1 0
2
0 0 0 0 0
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。
#include<iostream>
#include<cstdio>
#define N 500005
using namespace std;
int a[N];
int main(){
int n;
scanf("%d", &n);
for(int i=; i<=n; ++i)
scanf("%d", &a[i]);
int ans = ;
for(int i=; i<=n;){
int j = i;
while(j+<=n && a[j]!=a[j+])
++j;
//[i, j]这段区间需要作调整, 其中第i个位置和第j个位置的数字已经是stable
int len = j-i+;
if(a[i] == a[j]) {//形如01010
for(int k=i+; k<=j; ++k)
a[k] = a[i];
ans = max(ans, len/);
} else {//形如 010101
int k;
for(k=i+; k<=(i+j)/; ++k)
a[k] = a[i];
for(k; k<j; ++k)
a[k] = a[j];
ans = max(ans, len/-);
}
i = j+;
}
printf("%d\n", ans);
for(int i=; i<=n; ++i){
if(i!=) printf(" ");
printf("%d", a[i]);
}
printf("\n");
return ;
}
Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing的更多相关文章
- Codeforces Round #327 (Div. 2) B. Rebranding 水题
B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. ...
- Codeforces Round #327 (Div. 2) B Rebranding(映射)
O(1)变换映射,最后一次性替换. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; char s[ ...
- Codeforces Round #327 (Div. 2)
题目传送门 水 A - Wizards' Duel 题目都没看清就写了,1e-4精度WA了一次... /************************************************ ...
- 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 ...
- 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 ...
- Codeforces Round #327 (Div. 2)B(逻辑)
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- 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 ...
随机推荐
- css3 filter的十种特效
filter默认值是none,他不具备继承性,其中filter-function具有一下属性: grayscale灰度 sepia褐色(求专业指点翻译) saturate饱和度 hue-rotate色 ...
- 【转载】用Ionic开发hybrid APP
使用phonegap开发APP的优劣在此不必细说,快速,简单,跨平台,以及随着iOS,Android本身对webview优化所带来的不错的性能,便是其独有的优势.而且私以为在目前激烈而又变化快速的移动 ...
- Python之路第一课Day4--随堂笔记(迭代生成装饰器)
上节回顾: 1.集合 a.关系测试 b.去重 2.文件操作及编码 3.函数 4.局部变量和全局变量 上节回顾 本节课内容: 1.迭代器生成器 2.装饰器 3.json pickle数据序列化 4.软件 ...
- Spring4 实例
结构目录如下: 其中: dao层和entity层都属于hibernate的的管辖.entity层里面装的是每张表对应的持久化类.dao层里面装的是"底层操作数据库的行为",仅仅只是 ...
- Mac git提交步骤小记
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 19.0px; font: 13.0px "PingFang SC"; c ...
- 一篇关于匿名函数(function(){})()不错的文章
代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他 ...
- 使用Powershell收集服务器信息
function Get-OSInfo ([string]$Server) { $object = Get-WmiObject win32_computersys ...
- 学习笔记:Hashtable和HashMap
学了这么些天的基础知识发现自己还是个门外汗,难怪自己一直混的不怎么样.但这样的恶补不知道有没有用,是不是过段时间这些知识又忘了呢?这些知识平时的工作好像都是随拿随用的,也并不是平时一点没有关注过这些基 ...
- ORACLE冷备份与恢复
ORACLE备份和恢复有三种方式: (1)数据泵(expdp/impdp) (2)冷备份 (3)RMAN备份 就分类而言,(1)和(2)统有称为"冷"备份,(3)称为"热 ...
- IT人生知识分享:概率与运气
前言: 最近的人生多了些体验,也读了些许书,感觉还是有些知识是可以分享的. 今天难得周六,特意开电脑了,花几个小时写写,和大伙分享分享点知识. 以下内容,更多的需要读者思考,所以结论不会写太清晰,但一 ...