博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 591B Rebranding (模拟)
阅读量:5079 次
发布时间:2019-06-12

本文共 2591 字,大约阅读时间需要 8 分钟。

Rebranding

Problem Description

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

11 6

abacabadaba
a b
b c
a d
e g
f a
b b

Examples Output

cdcbcdcfcdc

题目链接:


题意:先输入 1<m,n<200000,m为原字符串的长度,n为变换规则的次数。再input长度为m的字符串。接下来输入n对(c1 c2)变换规则:每次变换规则(上次变换后的字符串的 c1变成c2 c2变成c1);

思路一:炸了

第一个想到的肯定是循环n次,每次都对上一个字符串中的 c1 c2进行变换。

每次变换需要循环m次去查找有没有等于c1,c2 的字符。

那么这个暴力模拟的时间复杂度就为O(n*m) o.o... 题目时间限制是 2000ms  而 m n 都是最大20完的数,所以肯定要炸喽...

思路二:

总体来想,假如原来的名字就是一个字符,经过变换最终到另外一个字符。

也就是说一个字符通过一系列会到一个确定的字符,而一个字符串就好比多个单个字符排在一起而已。

所以我们只需要模拟26个英文字母 经过一系列变换 最终变成了什么就可以了。


AC代码如下:

1 #include 
2 #include
3 using namespace std; 4 const int MAXN=200000+100; 5 char c[27]="abcdefghijklmnopqrstuvwxyz"; 6 char ss[MAXN]={
0}; 7 int main() 8 {
//a 97 b 98 9 int n,m;10 char c1,c2;11 scanf("%d%d%*c%s%*c",&n,&m,ss);12 for(int i=0;i

2017-03-09 22:03:35

转载于:https://www.cnblogs.com/Twobox/p/6528158.html

你可能感兴趣的文章
ES6内置方法find 和 filter的区别在哪
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
蓝桥杯-分小组-java
查看>>
Android Toast
查看>>
JAVA面试常见问题之Redis篇
查看>>
jdk1.8 api 下载
查看>>
getElement的几中属性介绍
查看>>
HTML列表,表格与媒体元素
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
设计器 和后台代码的转换 快捷键
查看>>
STL容器之vector
查看>>
数据中心虚拟化技术
查看>>
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>