Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

简介:
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
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:

 
   一道想法题目:无非就是字符 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'映射的字符。

#include<iostream> 
#include<cstdio>
#include<map>
#define N 200005
using namespace std;
char str[N];
int pos[30], ch[30];

int main(){
    int n, m;
    scanf("%d%d%s", &n, &m, str);
    getchar();
    for(int i=0; i<26; ++i)
        pos[i] = ch[i] = i;
    char s[4];
    while(m--){
        gets(s);
        if(s[0] == s[2]) continue;
        swap(ch[pos[s[0]-'a']], ch[pos[s[2]-'a']]);
        swap(pos[s[0]-'a'], pos[s[2]-'a']);
    }
    for(int i=0; i<n; ++i)
        printf("%c", ch[str[i]-'a']+'a');
    printf("\n");
    return 0;
}

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
4
0 0 1 1
output
0
0 0 1 1
input
5
0 1 0 1 0
output
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。


#include<iostream>
#include<cstdio>
#define N 500005
using namespace std;
int a[N];
int main(){
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; ++i)
        scanf("%d", &a[i]);
    int ans = 0;
    for(int i=1; i<=n;){
        int j = i;
        while(j+1<=n && a[j]!=a[j+1])
            ++j;
        //[i, j]这段区间需要作调整, 其中第i个位置和第j个位置的数字已经是stable
        int len = j-i+1;
        if(a[i] == a[j]) {//形如01010
            for(int k=i+1; k<=j; ++k) 
                a[k] = a[i];
            ans = max(ans, len/2);
        } else {//形如 010101
            int k;
            for(k=i+1; k<=(i+j)/2; ++k)
                a[k] = a[i];
            for(k; k<j; ++k)
                a[k] = a[j];
            ans = max(ans, len/2-1);
        } 
        i = j+1;
    }
    printf("%d\n", ans);
    for(int i=1; i<=n; ++i){
        if(i!=1) printf(" ");
        printf("%d", a[i]);
    }
    printf("\n");
    return 0;
}


目录
相关文章
|
Kubernetes Java Serverless
进击的 Serverless:Java 应用如何从容地面对突增流量
进击的 Serverless:Java 应用如何从容地面对突增流量
104374 86
|
SQL 物联网 Apache
使用Apache IoTDB进行IoT相关开发的架构设计与功能实现(11)
目前,IoTDB中不存在冲突的权限,因此用户的真正权限是用户自身权限和用户角色权限的结合。也就是说,要确定用户是否可以执行操作,取决于用户自己的权限之一或用户角色的权限是否允许该操作。用户自己的特权和用户角色的特权可能会重叠,但这并不重要。
454 22
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
3月前
|
存储 机器学习/深度学习 安全
阿里云服务器租用价格:最新包年、按小时、按月收费标准及活动价格参考
阿里云服务器租用价格参考,轻量应用云服务器2核2G38元1年,云服务器2核2G3M99元1年、2核4G5M199元1年。本文为大家展示阿里云服务器最新包年、按小时、按月收费标准,以及部分云服务器的最新活动价格情况,以供大家了解阿里云服务器的收费模式,不同实例之间的收费差异,从而根据自己的需求和预算等情况选择适合自己的云服务器实例规格和配置。
|
9月前
|
云安全 机器学习/深度学习 人工智能
课时12:阿里云安全产品之态势感知
阿里云态势感知是基于人工智能的安全产品,帮助企业应对高隐蔽性网络攻击。它通过机器学习全面感知网络威胁,覆盖网络层、主机层和应用层,提供实时入侵检测与响应。具备威胁模型、专家定制、超强检索及全网威胁情报等六大核心优势,显著增强企业网络安全防御能力。在G20峰会期间,成功实现平台用户网站安全运营零干扰。
474 0
|
并行计算 Java 大数据
Java函数式编程:一场编程范式的革命,让你的代码焕发新生!
【8月更文挑战第30天】Java函数式编程是一种基于数学函数理论的编程范式,强调数据处理的不可变性和纯函数使用,通过将函数视为第一类对象,实现更简洁、易读的代码结构,在数据流处理与并行计算中尤为突出。与命令式编程关注执行步骤不同,函数式编程侧重描述计算目标而非具体操作流程,减少了状态变化,使代码更清晰易维护。在Java中,函数式编程通过降低副作用和状态依赖简化了复杂度,并提高了代码质量和测试性,尤其是在Java 8的Stream API中得到了充分体现,能够自动优化多核处理器上的并行处理性能。
232 3
|
应用服务中间件
Tomcat打不开startup.bat
Tomcat打不开startup.bat
317 2
|
Cloud Native Java 开发者
新一代Java框架Quarkus的性能优化与应用
新一代Java框架Quarkus的性能优化与应用
|
机器学习/深度学习 人工智能 运维
智能化运维:AI在故障预测与自愈系统中的应用
【6月更文挑战第4天】本文探讨了人工智能(AI)技术在运维领域的革新作用,特别是其在故障预测和自愈系统中的应用。通过分析AI技术的基本原理及其在运维中的实际应用案例,文章揭示了AI如何提升系统的稳定性和效率,同时指出了实施过程中的挑战和未来的发展方向。
|
SQL Oracle 关系型数据库
实时计算 Flink版产品使用合集之使用JDBC方式读取Oracle的number类型时,通过什么方式进行映射
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
432 0
实时计算 Flink版产品使用合集之使用JDBC方式读取Oracle的number类型时,通过什么方式进行映射