Codeforces 591 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 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.
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.
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:
一个公司有一个自己的起始商标,然后它请来了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的更多相关文章
- codeforces 591 E. Three States(bfs+思维)
题目链接:http://codeforces.com/contest/591/problem/E 题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道 ...
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- Codeforces Round #504:D. Array Restoration
D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...
- Codeforces 571B Minimization:dp + 贪心【前后相消】
题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...
- Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】
题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...
- Codeforces 180C Letter:dp
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小 ...
- Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle
题目传送门 题目大意:Bash喜欢对数列进行操作.第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到.第二种操作是将ai修改为x.总共Q个询问,N个数. Soluti ...
- Educational Codeforces Round 55:B. Vova and Trophies
B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...
- 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是可以跳的格数,注 ...
随机推荐
- Centos6.X安装桌面
1.前置环境yum -y groupinstall 'X Window System'2.桌面安装 yum -y groupinstall 'Desktop' 3.语言包yum -y groupins ...
- Mybatis学习day2
Mybatis初探 之前已经用利用mybatis实现链接数据库查询所有用户的信息(用的是在resources下建立和Dao层一样目录的xml实现的).这次再来看一下增删改查等其它的操作. 利用Myba ...
- 使用xshell给阿里云服务器安装mysql
1. 安装mysql https://www.jianshu.com/p/ce53320dcfbd 2. 获取mysql 5.7随机生成的密码 https://www.yanning.wang/arc ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:用户管理与权限分配
create user mr identified by mrsoft default tablespace users temporary tablespace temp; create user ...
- day8 文件的读取
只读 只写 追加 读写 功能 username = input('请输入你要注册的用户名:') password = input('请输入你要注册的密码:') with open('list_of_i ...
- Java面向对象编程 -1
面向对象简介 C语言是面向过程开发的代表 C++ 或者Java 是面向对象的编程语言 所谓的面向过程指的是面对于一个问题的解决方案,更多的情况下是不会做出重用的设计思考的. 而面向对象的主要设计形式是 ...
- 使用java实现二叉查找树的插入,修改和删除方法
目前使用的是根据key的hashcode来进行排序,并且没有考虑hash碰撞的问题 package com.zhou.tree; import java.util.Comparator; import ...
- linux中cp指令前面加反斜杠
在cp指令前面加反斜杠可以不弹出是否覆盖的询问而直接覆盖! 如:cp /app/WEB-INF/com/cfg.properties /app_bak/WEB-INF/com/cfg.properti ...
- 自定义Toast排队重复显示问题:
原文 http://blog.csdn.net/baiyuliang2013/article/details/38655495Toast是安卓系统中,用户误操作时或某功能执行完毕时,对用户的一种提示, ...
- webpack配置文件里loader的执行顺序:从下到上,从右到左; css-loader开启css模块化modules: true,
注释: options:{ importLoaders: 2 } 解决样式文件里使用@import 'xxx.xxx' 的问题 module: { rules: [{ test: /\.scss$/, ...