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& ...
随机推荐
- Windows下Memcached的安装配置方法
1.将第一个包解压放某个盘下面,比如在c:\memcached. 2.在终端(也即cmd命令界面)下输入 'c:\memcached\memcached.exe -d install' 安装. 3.再 ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 实现一个简单的Plugin
1.准备 1)安装pluginlib_tutorials包: 2)创建一个包 2.创建基类 创建一个基类,所有的插件都将从基类继承,如RegularPolygon基类 创建一个基类头文件 其内容如下 ...
- 如何确定tabcontrol哪一页码是活跃页???
tabControl1.SelectedIndex属性 显示了现在显示的是哪一页码内的控件.
- 使用dom解析器对xml文档内容进行增删查改
直接添代码: XML文档名称(one.xml) <?xml version="1.0" encoding="UTF-8" standalone=" ...
- Android内置和外置SD卡的位置获取
public class StorageUtils { private static String TAG="123"; // 获取主存储卡路径 内置内存卡路径 public st ...
- Javascript脚本 :Function 对象的定义和使用
javascript Function 对象的定义 创建函数的语法:var myFunction=new Function(arg1,arg2,...agrN,body);agrN 为函数的参数,b ...
- TSQL--删除正在运行的数据库
); SET @dbName='DB1_SNAP' BEGIN TRY --===================================== --查找当前数据库所有连接并删除 DECLARE ...
- js工具库简单介绍
javascript mvc的解决方案: angularjs, backbone,underscore, 有空的时候了解一下. 移动端的几个需要了解一下,jq mobile, zepto.knocko ...