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. tomcat配置限制ip和建立图片服务器

    1.配置限制ip访问 打开 tomcat里conf文件下的server.xml 在<Host name="localhost" appBase="webapps&q ...

  2. js面试必考:this

    this是前端面试中必考的基础知识点,也是新手小白在做项目中经常晕头转向的问题.但其实this不难理解. 判断this指向时,记住以下几点: 判断函数类型, 1.1 如果是箭头函数,则为第一个包裹箭头 ...

  3. C++赋值操作符不能继承

    在网上搜索相关资料时,看到有人说,赋值操作符可以被继承,并且给出了一个例子. 一时间,也没想出那个例子错在哪里. 例子代码如下: #include <iostream> using nam ...

  4. PPT页面动画制作

    因为武汉新型冠状肺炎的影响,今年自从2月3号开工以来,就在家办公.我的任务刚好是安排做PPT,虽说之前做过PPT,但大家都知道,作为一个IT测试工程师,更多的是测试工作,只有在培训,还有年终汇报的时候 ...

  5. JavaScript 数字

    数字(Number)也称为数值或数. 数值直接量 当数字直接出现在程序中时,被称为数值直接量.在 JavaScript 程序中,直接输入的任何数字都被视为数值直接量. 示例1 数值直接量可以细分为整型 ...

  6. STM32单片机的软件重启和远程重启

    STM32单片机可以通过以下代码实现重启(core_cm3.h).同时如果利用AT命令进行无线通讯,服务器后台和客户端之间用MODBUS通讯即4G+MODBUS RTU,可以利用F05写单个线圈的方法 ...

  7. ssm 框架 使用ajax异步,实现登陆

    只是简单写一下 js.jsp.和controller jsp <%@ page contentType="text/html;charset=UTF-8" language= ...

  8. 144、Java链表之定义一个Node类并输出

    01.代码如下: package TIANPAN; class Node { // 每一个链表实际上就是由多个节点组成的 private String data; // 要保存的数据 private ...

  9. 无线冲方案 MCU vs SoC

    参考: MCU还是SoC:2018无线充电技术走向何方? 方案 分立/集成 特点 框图 MCU方案 分立方案 MCU芯片负责Qi协议的运算和外围电路控制,ST MCU居多:单线圈,双线圈,三线圈都有: ...

  10. C++ 类构造函数 & 析构函数

    前言: 析构函数和构造函数是一对.构造函数用于创建对象,而析构函数是用来撤销对象.简单的说:一个对象出生的时候,使用构造函数,死掉的时候,使用析构函数.构造函数 和 析构函数 各有各的用途,在构造函数 ...