B. Rebranding

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/591/problem/B

Description

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.

Sample Input

6 1
police
p m

Sample Output

molice

HINT

题意

长度为n的字符,有m次操作,每次操作会将所有的x字符变成y字符,将所有的y字符变成x字符

题解:

变化,我们不要在原来的长度为n的字符里面去变,我们就在26个字符里面变就好了

复杂度是mO(26)或者mO(1)

代码

#include<iostream>
#include<stdio.h>
using namespace std; int a[];
int b[];
string s,x,y;
int main()
{
int n,m;
cin>>n>>m;
cin>>s;
for(int i=;i<;i++)
a[i]=b[i]=i;
for(int i=;i<m;i++)
{
cin>>x>>y;
if(x[]==y[])continue;
swap(a[b[x[]-'a']],a[b[y[]-'a']]);
swap(b[x[]-'a'],b[y[]-'a']);
}
for(int i=;i<n;i++)
{
int d = (s[i]-'a');
printf("%c",a[d]+'a');
}
printf("\n");
}

最新文章

  1. Java--JDK动态代理核心源码解析
  2. iOS调用HTML
  3. DevExpress TreeList 那些事儿
  4. Windows 桌面软件:不绑定bing搜索的缤纷桌面
  5. 调用支付宝PHP接口API实现在线即时支付功能(UTF-8编码)
  6. android 设置半透明
  7. iOS 提示音播放
  8. 新手介绍简单一下iOS开发中几种界面传值
  9. ORACLE IMPDP导入报表数据已存在
  10. centos6安装openresty
  11. RMQPOJ3264
  12. Arcgis map export or print Error: Cannot map metafile into memory. Not enough memory
  13. 一分钟了解Android横竖屏 mdpi hdpi xhdpi xxhdpi xxxhdpi (转)
  14. Wireshark 显示域名列
  15. 二)spring 集成 ehcache jgroups 集群
  16. TCP并发server模型(三)
  17. map、reduce处理数据结构及常见案例
  18. 2806 红与黑 个人博客:doubleq.win
  19. 电商网站中价格的精确计算(使用BigDecimal进行精确运算(实现加减乘除运算))
  20. EJB 配置多个数据源

热门文章

  1. Oracle数据库表结构导出
  2. Oracle OCI-22053:溢出错误解决方法
  3. acdream 1044
  4. jQuery常用的正则表达式
  5. STM32F407 外扩SRAM
  6. TP分析
  7. 使用Google Chart API绘制组合图
  8. 个人思考:能否sub.prototye=sup.prototype实现继承
  9. openstack domain serverID connect uri
  10. [VS2012]无法新建或者编译已有的项目