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& ...
随机推荐
- 场景中,并没有灯源的存在,但是cube却会有灯光照射的反应,这就是Light Probe Group的作用。
http://blog.csdn.net/qq617119142/article/details/41674755
- Deepin 2014.2正式版发布 - 自由·独特·前卫
感谢 deepin 的投递 deepin致力于为全球用户提供美观易用.安全可靠的Linux系统. deepin系统使用基于HTML5技术开发的深度桌面环境,搭配深度音乐.深度影院.WPS和搜狗输入法等 ...
- Zephyr Introduction
wiki Importer Workflow wiki https://zephyrdocs.atlassian.net/wiki/display/ZTD/Zephyr+for+JIRA+Docume ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Ajax——jQuery实现
紧接上文 一.load()方法 load() 方法师jQuery中最为简单和常用的Ajax方法,能载入远程的HTML代码并插入到DOM中.它的机构是:load(url[,data][,callback ...
- SVN下载地址及注意事项
SVN下载地址:VisualSVN:http://www.visualsvn.com/server/download 服务器端(添加仓库和用户)TortoiseSVN:http://torto ...
- UIPopoverController 简单用法(全代码)
AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDic ...
- JAVA WEB第0课
在这学期内要掌握JAVA WAB高级网站开发的所有知识,并可以实际运用到.每周将花费20小时左右的时间来学习此门课程,每一天,在当天其他课程任务完成后将开始学习该课程,具体时间要看当天 ...
- React+gulp+browserify模块化开发
阅读本文需要有React的基础知识,可以在React 入门实例教程和React中文官网进行基础学习. 没有React基础也可以学习本文,本文主要不是学习React,而是gulp+browserify进 ...
- .net core in Docker 部署方案(随笔)
前一段时间由于项目需要 .net core 在docker下的部署,途中也遇到很多坑,看了各同行的博客觉得多多少少还是有些问题,原本不想写此篇文章,由于好友最近公司也需要部署,硬是要求,于是花了些时间 ...