B. Rebranding
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 lettersxi and 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:

一个公司有一个自己的起始商标,然后它请来了m位设计师,每位设计师都会把之前的商标中,字母x换成字母y,把字符y换成字母x。问经过了m位设计师的折腾之后,最终的商标是什么。

这题一开始想麻烦了。

一共就26个字符,一开始每一个字母都只对应自己,然后每一位设计师来了就change一下,循环每一个字母,看哪一个是设计师要change的那一个,change掉,注意别弄重复了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int len, m;
char ha[200];
int flag[200];
char st[200005]; void init()
{
int i;
for (i = 0; i < 30; i++)
{
ha[i] = i;
}
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); init();
int i, j;
char s[10], e[10];
scanf("%d%d", &len, &m);
scanf("%s", st); for (j = 0; j < m; j++)
{
scanf("%s %s", &s, &e); memset(flag, 0, sizeof(flag));
for (i = 0; i < 28; i++)
{
if (ha[i] == s[0] - 'a'&&flag[i] != 2)
{
ha[i] = e[0] - 'a';
flag[i] = 1;
}
if (ha[i] == e[0] - 'a'&&flag[i] != 1)
{
ha[i] = s[0] - 'a';
flag[i] = 2;
}
}
}
for (i = 0; i < len; i++)
{
printf("%c", ha[st[i] - 'a'] + 'a');
}
printf("\n");
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 591 B:Rebranding的更多相关文章

  1. codeforces 591 E. Three States(bfs+思维)

    题目链接:http://codeforces.com/contest/591/problem/E 题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道 ...

  2. Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)

    http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...

  3. Codeforces Round #504:D. Array Restoration

    D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...

  4. Codeforces 571B Minimization:dp + 贪心【前后相消】

    题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...

  5. Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】

    题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...

  6. Codeforces 180C Letter:dp

    题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小 ...

  7. Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle

    题目传送门 题目大意:Bash喜欢对数列进行操作.第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到.第二种操作是将ai修改为x.总共Q个询问,N个数. Soluti ...

  8. Educational Codeforces Round 55:B. Vova and Trophies

    B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...

  9. Educational Codeforces Round 55:A. Vasya and Book

    A. Vasya and Book 题目链接:https://codeforc.es/contest/1082/problem/A 题意: 给出n,x,y,d,x是起点,y是终点,d是可以跳的格数,注 ...

随机推荐

  1. [运维] 如何将 Linux 上的 nginx 变成 静态资源服务器 (二)

    环境 虚拟机上运行 Linux centos 7 64 已经安装 nginx-1.16.1.tar.gz  具体的安装过程可以参考 https://www.cnblogs.com/unityworld ...

  2. Linux下安装 boost 库

    1. 先去官网下载压缩包: https://www.boost.org/ 2. 解压 tar -zvxf boost_1_70_0.tar.gz 2. cd 进入根目录,然后执行: ./bootstr ...

  3. ios 物流时间轴,自动匹配电话号码,可点击拨打

    http://www.code4app.com/thread-27587-1-1.html 资讯时间轴(折叠/展开) http://www.code4app.com/thread-32358-1-1. ...

  4. 前端代码编译器Hbuilder下载地址和谷歌浏览器下载地址

    编译器:HbuilderX 浏览器:谷歌浏览器

  5. Spark教程——(9)Oozie编排Spark任务

    进入Hue管理界面,打开Oozie Editor: 将打包好的Spark程序上传到HDFS上,拖拽Spark任务,编辑任务属性,选择打包好的Spark程序,设置主函数所在类,设置选项参数: 保存为任务 ...

  6. 时间和日期实例-<Calender计算出生日期相差几天>

    String day1="1994:10:04"; String day2="1994:10:03"; SimpleDateFormat format= new ...

  7. 修改vue中的挂载页面(index.html)的路径

    修改vue中的挂载页面(index.html)的路径 2019年03月30日 12:07:12 VegasLemon 阅读数 501    版权声明:本文为博主原创文章,未经博主允许不得转载. htt ...

  8. 企业面试问题收集-ssh框架

    SSH框架阶段 SSH的优缺点,使用场景? Hibernate优缺点 Hibernate优点:(1) 对象/关系数据库映射(ORM)它使用时只需要操纵对象,使开发更对象化,抛弃了数据库中心的思想,完全 ...

  9. [经验] Unity3D 里怎么制作天空盒(skybox)

    记载一个简单的  天空盒子  的制作方法 第一步: 在 assets 文件夹下新建一个文件夹, 随便取个名字, 不过最好是用来专门管理场景游戏对象的文件夹,    例如放在这个 Skybox 里:  ...

  10. 在PyCharm中自动添加文件头、时间日期等信息

    初次安装使用PyCharm,在新建.py文件时会发现文件头并没有什么信息,因此,使用模板会比较方便.方法如下: 1.打开PyCharm,选择File--Settings 2.依次选择Editor--- ...