Rebranding(模拟+思维)
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.
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 letters xi and yi.
Output
Print the new name of the corporation.
Examples
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
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
string str;
char a[35];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,m;
cin>>n>>m;
cin>>str;
for(int t=0;t<26;t++)
{
a[t]='a'+t;
}
string s1,s2;
int vis[35];
for(int t=0;t<m;t++)
{
cin>>s1>>s2;
memset(vis,0,sizeof(vis));
for(int j=0;j<26;j++)
{
if(a[j]==s1[0])
{
vis[j]=1;
a[j]=s2[0];
}
}
for(int j=0;j<26;j++)
{
if(a[j]==s2[0]&&vis[j]==0)
{
a[j]=s1[0];
}
}
}
for(int t=0;t<n;t++)
{
cout<<a[str[t]-'a'];
}
return 0;
}
Rebranding(模拟+思维)的更多相关文章
- 模拟+思维 HDOJ 5319 Painter
题目传送门 /* 题意:刷墙,斜45度刷红色或蓝色,相交的成绿色,每次刷的是连续的一段,知道最终结果,问最少刷几次 模拟+思维:模拟能做,网上有更巧妙地做法,只要前一个不是一样的必然要刷一次,保证是最 ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. ...
- HDU 5538 House Building(模拟——思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...
- atcoder C - Snuke and Spells(模拟+思维)
题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_c 题解:就是简单的模拟一下就行.看一下代码就能理解 #include <iostream& ...
- codeforces 816 C. Karen and Game(模拟+思维)
题目链接:http://codeforces.com/contest/816/problem/C 题意:给出一个矩阵,问能否从都是0的情况下只将一整行+1或者一整列+1变形过来,如果可以输出需要步数最 ...
- CodeForces 625B 字符串模拟+思维
题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...
- UVA 1030 - Image Is Everything【模拟+思维+迭代更新】
题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...
- UVA 10881 - Piotr's Ants【模拟+思维】
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- php扩展开发1--添加函数
目标:便携php扩展 要求实现 输出hello word 首先用的是php7.0.3 centos7.1或者centos6.+ 1.1 RPM安装PHP rpm -Uvh https://mirr ...
- 7-python自定义opener
Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就是模块帮我们构 ...
- 通明讲JDBC(一)–认识JDBC
本章记录了jdbc的简单使用方式! 0,jdbc的作用 1,jdbc入门准备工作 2,jdbc注册驱动 3,使用jdbc对数据库CRUD 0,jdbc的作用 与数据库建立连接.发送操作数据库的语句并处 ...
- 用Linq取两个数组的差集
两个数组,取其差集,用Linq做比较方便,效率也比较高,具体如下示例 有两个数组list1 和list2 ,如下 List<int> list1 = new List<int> ...
- 通过event事件来控制红绿灯通行车辆
事件的初始值为False,所以最开始就是红灯,先模拟红绿灯的规律,设定为每两秒变换一次灯,然后再模拟车辆通行,通过事件来将两者的事件结合起来, 当事件为False时,为红灯,车辆处于等待状态,一直wa ...
- Visual Studio 2010调试本地 IIS 站点
点击vs的Debug-Attach to Process选中 w3wp.exe,然后点击Attach, vs便进入debug模式.
- java IO 对象流 反序列化和序列化
例: 重点:需要序列化的对象必须实现Serializable接口 //需要序列化的对象 public class User implements Serializable { private Stri ...
- Deep Visual-Semantic Alignments for Generating Image Descriptions(深度视觉-语义对应对于生成图像描述)
https://cs.stanford.edu/people/karpathy/deepimagesent/ Abstract We present a model that generates na ...
- Ubuntu16.04切换python3和python2
切换Python3为默认版本: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo ...
- CentOS 用户/组与权限
useradd:添加用户 useradd abc,默认添加一个abc组 vipw:查看系统中用户 groupadd:添加组groupadd ccna vigr:查看系统中的组 gpasswd:将用户a ...