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

  1. 6 1
  2. police
  3. p m

Output

  1. molice

Input

  1. 11 6
  2. abacabadaba
  3. a b
  4. b c
  5. a d
  6. e g
  7. f a
  8. b b

Output

  1. cdcbcdcfcdc

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<stack>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<cmath>
  11. const int maxn=1e5+5;
  12. typedef long long ll;
  13. using namespace std;
  14. string str;
  15. char a[35];
  16. int main()
  17. {
  18. std::ios::sync_with_stdio(false);
  19. std::cin.tie(0);
  20. int n,m;
  21. cin>>n>>m;
  22. cin>>str;
  23. for(int t=0;t<26;t++)
  24. {
  25. a[t]='a'+t;
  26. }
  27. string s1,s2;
  28. int vis[35];
  29. for(int t=0;t<m;t++)
  30. {
  31. cin>>s1>>s2;
  32. memset(vis,0,sizeof(vis));
  33. for(int j=0;j<26;j++)
  34. {
  35. if(a[j]==s1[0])
  36. {
  37. vis[j]=1;
  38. a[j]=s2[0];
  39. }
  40. }
  41. for(int j=0;j<26;j++)
  42. {
  43. if(a[j]==s2[0]&&vis[j]==0)
  44. {
  45. a[j]=s1[0];
  46. }
  47. }
  48. }
  49. for(int t=0;t<n;t++)
  50. {
  51. cout<<a[str[t]-'a'];
  52. }
  53. return 0;
  54. }

Rebranding(模拟+思维)的更多相关文章

  1. 模拟+思维 HDOJ 5319 Painter

    题目传送门 /* 题意:刷墙,斜45度刷红色或蓝色,相交的成绿色,每次刷的是连续的一段,知道最终结果,问最少刷几次 模拟+思维:模拟能做,网上有更巧妙地做法,只要前一个不是一样的必然要刷一次,保证是最 ...

  2. codeforces 591B Rebranding (模拟)

    Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...

  3. Codeforces Round #327 (Div. 2) B. Rebranding 模拟

    B. Rebranding   The name of one small but proud corporation consists of n lowercase English letters. ...

  4. HDU 5538 House Building(模拟——思维)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...

  5. atcoder C - Snuke and Spells(模拟+思维)

    题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_c 题解:就是简单的模拟一下就行.看一下代码就能理解 #include <iostream& ...

  6. codeforces 816 C. Karen and Game(模拟+思维)

    题目链接:http://codeforces.com/contest/816/problem/C 题意:给出一个矩阵,问能否从都是0的情况下只将一整行+1或者一整列+1变形过来,如果可以输出需要步数最 ...

  7. CodeForces 625B 字符串模拟+思维

    题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...

  8. UVA 1030 - Image Is Everything【模拟+思维+迭代更新】

    题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...

  9. UVA 10881 - Piotr's Ants【模拟+思维】

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. Loadrunner Analyze

    Analysis 对controller运行的结果进行分析 1.首先必须明确:光靠Analysis是不行的,只要能通过Analysis分析出部分问题就已经很不错了, 善于利用它才是最关键的. 2.如何 ...

  2. 【项目运行异常】BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking ...

  3. 关于recv的返回值

    通常recv有几种返回值 1.==0 表示收到FIN包, 因为FIN包,是状态为标记为FIN的空包,没有携带数据,所以recv的长度为0 2.>0 表示收到了数据, 但是有没有收完,是不知道的 ...

  4. Ubuntu 源使用帮助

    地址 https://mirrors.ustc.edu.cn/ubuntu/ 说明 Ubuntu 软件源 收录架构 AMD64 (x86_64), Intel x86 其他架构请参考 Ubuntu P ...

  5. django: rest-framework的 分页和过滤

    django: rest-framework的 分页和过滤 2018年06月28日 10:09:01 weixin_42359464 阅读数:136 标签: flaskrestframeworkdja ...

  6. EF 常见语句以及sql语句简单 后续继续添加

    1.注意级联删除的时候数据库的外键要设置为开启级联删除,(数据库里sqlserver的外键修改的时候,可以看到级联删除和级联更新) using System;using System.Collecti ...

  7. Charles常见问题

    Charles常见问题汇总 Charles是一款很好用的抓包修改工具,但是如果你不是很熟悉这个工具的话,肯定会遇到各种感觉很莫名其妙的状况,这里就来帮你一一解答下面再说说charles的一些其他常用的 ...

  8. App测试从入门到精通之功能测试

    App的功能测试指的是针对软件需求以及用户要求针对APP功能进行测试.简单点理解就是保证App功能的正确性,不要系统出现Bug.让用户用户的舒服,用的爽!好了,我们看下关于App的功能测试要点有哪些? ...

  9. vmware ubuntu安装vmware tools

    vmware tools可以说是其平台虚拟机必不可少的工具,可以使母机(你的电脑)通过复制粘贴向虚拟机中传递文件信息,对我们虚拟机的使用由非常大的帮助,当然也可以通过使用共享硬盘来共享文件,但操作起来 ...

  10. POJ - 3984 迷宫问题 BFS求具体路径坐标

    迷宫问题 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...