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是可以跳的格数,注 ...
随机推荐
- 什么是redis事务
一.什么是redis事务? 可以一次性执行多条命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其他命令 二.Redis 事务可以做什么? 一个队列中, ...
- 华为事件对A股的影响思考
美国对华为实施禁商令: 利好:自主可控-替代品 软件:国产操作系统(中国软件,浪潮软件,湘邮科技...) 芯片:国产芯片(士微兰,国民技术...) 利好:华为优势产品 5G: 利好:反制资源 稀土永磁 ...
- c++子类父类关系(翁恺c++公开课[15-16]学习笔记)
关于类的继承有三种:public继承.private继承.protected继承 首先说明,关于类的成员变量.函数的权限有三种(public.private.protected) 我们通常会让所有的成 ...
- Android之Builder对话框的一些常用方式
原文: http://blog.csdn.net/kkfdsa132/article/details/6322835 Android为我们提供几种对话框,主要有:AlertDialog.Progres ...
- 重構電影網源碼 1905.com - 數據庫結構表
最近閒來無事,想著克隆一個電影網站. WWW.ROAK.COM 技術語言:JAVA EE * j2ee核心组件:jsp.servlet.jdbc.ejb.jndi * 数据通信:xml标记语言 * ...
- ch8 固定宽度、流式、弹性布局
假设浏览器窗口设置为1250px:wrapper的宽度为960px:content的宽度为920px:确保了wrapper居中时两边有20px的间距: secondary的宽度为230px: pr ...
- CSS - 插入图片img和背景图片
1. img插入图片,用的最多,比如产品展示类 .section img { width: 200px;/* 插入图片更改大小 width 和 height */ height: 210px; mar ...
- 2020-2-18 restful的学习
1-1 restful简介及资源的介绍 restful 是什么? 本质:一种软件架构风格 核心:面向资源 解决的问题: 1. 降低开发的复杂性 2. 提高系统的可伸缩性 设计概念和 ...
- 数据库事务ACID特效
一.数据库事务正确执行的4个基础要素: 1.原子性 整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状 ...
- Windows驱动开发-派遣函数
一个简单的派遣函数格式 NTSTATUS DispatchFunction(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { //业务代码区 //设置返回状态 pI ...