B. Rebranding
2 seconds
256 megabytes
standard input
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 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.
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.
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:
不能暴力搜索,暴力会TLE
将其转化为在26个英文字母表里的序号位置,通过交换位置来实现字母的交换,最终输出对应的字母
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h> using namespace std; const int N = ; char s[N]; int main()
{
char a[], b[];
int n, m, i, q[];
while(~scanf("%d%d", &n, &m))
{
scanf("%s", s);
for(i = ; i < ; i++)
q[i] = i;
while(m--)
{
scanf("%s%s", a, b);
int x = a[] - 'a';
int y = b[] - 'a';
for(i = ; i < ; i++)
{
if(q[i] == x)
q[i] = y;
else if(q[i] == y)
q[i] = x;
}
}
for(i = ; s[i] != '\0' ; i++)
printf("%c", q[s[i] - 'a'] + 'a');
printf("\n");
}
return ;
}
B. Rebranding的更多相关文章
- 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. T ...
- Codeforces Round #327 (Div. 2) B. Rebranding 水题
B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...
- cf591B Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- ural 2032 Conspiracy Theory and Rebranding (数学水题)
ural 2032 Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 ...
- Rebranding(模拟+思维)
The name of one small but proud corporation consists of n lowercase English letters. The Corporation ...
- Rebranding(字母代换)
个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时. 所以我就是将输入的字母从a到z的数目和路径依次保存,再建立一个book数组表示字母现在所指的字母 ,一开始就直接 ...
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. ...
- Codeforces 591 B:Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- LA 3971 (二分) Assemble
题意: 你有b块钱想要组装一台电脑.给出n个配件的种类,品质和价格,要求每个种类的配件各买一个总价格不超过b且“品质最差配件”的品质因子应尽量大. 这种情况下STL的map的确很好用,学习学习 这种最 ...
- android4.0以上部分手机绘图时会出现重影
canvas外层的div需要设定属性style="overflow:visible;-webkit-transform: translateZ(0);
- 用vs2012的命令利用xsd文件生成对应的C#类,把xml的string类型映射到生成的类
输入命令: xsd d:\TDDOWNLOAD\atom-author-link.xsd /c /language:C# /outputdir:d:\ 含义: 将d:\TDDOWNLOAD\atom- ...
- 嵌入式 Linux下编译并使用curl静态库
#x86 ./configure --disable-shared --enable-static --disable-ftp --disable-ipv6 --disable-rtsp --disa ...
- HDU 4832 Chess
Chess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- Yii系列教程(三):集成Redis
1安装Redis 切换至/usr/local/src下,下载并安装redis: $ wgethttp://redis.googlecode.com/files/redis-2.6.12.tar.gz ...
- 怎样学法学?——民法学泰斗王利明教授的演讲 z
今晚我讲“怎样学习法律”,但不是讲一般的学习法学的方法,而是主要从法学.法律的特征讲起.(因为)我们学习任何东西,都首先要搞清楚我们的学习对象有什么特征.性质. 我们要了解法律.法学本身的性质,要了解 ...
- LR 解决中文乱码(来源——百度)
因为我们使用的中文操作系统默认的中文编码格式是GB2312,所以LR对服务器的返回内容自动使用GB2312方式阅读的,但是几乎所有的中文网站现在都在使用UTF-8的方式来编码,由于解码编码的方式不同最 ...
- linux笔记_20150825_linux下的软件工具唠叨下
这些都是书上看到的,有些工具我也没有完全用过.先记下来再说.闲着也是闲着. 1.linux下常见的语言及编程环境:c/c++/java/perl/fortan等. 2.图形环境:gnome/kde/g ...
- Intent传输数据的补充
发现用intent的putExtra()或者putExtras()传输的都是基本数据类型. 如果要传输自定义数据类型,就要用到其他方法,老罗介绍的大概有3种: 1. 静态变量 2. 全局变量 3. ...