Rebranding

Problem 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.

Examples Input

11 6
abacabadaba
a b
b c
a d
e g
f a
b b

Examples Output

cdcbcdcfcdc

题目链接:http://codeforces.com/problemset/problem/591/B


题意:先输入 1<m,n<200000,m为原字符串的长度,n为变换规则的次数。再input长度为m的字符串。接下来输入n对(c1 c2)变换规则:每次变换规则(上次变换后的字符串的 c1变成c2 c2变成c1);

思路一:炸了

第一个想到的肯定是循环n次,每次都对上一个字符串中的 c1 c2进行变换。

每次变换需要循环m次去查找有没有等于c1,c2 的字符。

那么这个暴力模拟的时间复杂度就为O(n*m) o.o... 题目时间限制是 2000ms  而 m n 都是最大20完的数,所以肯定要炸喽...

思路二:

总体来想,假如原来的名字就是一个字符,经过变换最终到另外一个字符。

也就是说一个字符通过一系列会到一个确定的字符,而一个字符串就好比多个单个字符排在一起而已。

所以我们只需要模拟26个英文字母 经过一系列变换 最终变成了什么就可以了。


AC代码如下:

 #include <iostream>
#include <cstdio>
using namespace std;
const int MAXN=+;
char c[]="abcdefghijklmnopqrstuvwxyz";
char ss[MAXN]={};
int main()
{//a 97 b 98
int n,m;
char c1,c2;
scanf("%d%d%*c%s%*c",&n,&m,ss);
for(int i=;i<m;i++)
{
scanf("%c%*c%c%*c",&c1,&c2);
for(int j=;j<;j++)
{
if(c[j]==c1)
c[j]=c2;
else if(c[j]==c2)
c[j]=c1;
}
}
for(int i=;i<n;i++)
printf("%c",c[ss[i]-]);
cout<<endl;
return ;
}

2017-03-09 22:03:35

codeforces 591B Rebranding (模拟)的更多相关文章

  1. CodeForces 591B Rebranding

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...

  2. 字符串 || CodeForces 591B Rebranding

    给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少 arr数组记录每个字母最后被替换成了哪个字母 读入字符前面加一空格 scanf(" %c %c", &am ...

  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. Codeforces 389B(十字模拟)

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  5. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  6. Codeforces 631C. Report 模拟

    C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  7. Codeforces 679B. Barnicle 模拟

    B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...

  8. CodeForces 382C【模拟】

    活生生打成了大模拟... #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsig ...

  9. codeforces 719C (复杂模拟-四舍五入-贪心)

    题目链接:http://codeforces.com/problemset/problem/719/C 题目大意: 留坑...

随机推荐

  1. mysql编程---函数

    (存储)函数: 函数,也说成"存储函数",其实就是js或php中所说的函数! 唯一的区别: 这里的函数必须返回一个数据(值): 定义形式: 注意事项: 1, 在函数内容,可以有各种 ...

  2. 使用Android Studio导入第三方库项目

    在使用Android Studio开发时,用到了第三方库SlidingMenu(现在已经不推荐使用了),尽管如此,但具体怎么导入第三方库还是需要知道的,在查阅各种资料后,知道了一种比较容易可行的方法 ...

  3. 极客君教你破解隔壁妹子的wifi密码,成功率高达90%

    首先,给大家推荐一个我自己维护的网站: 开发者网址导航:http://www.dev666.com/ 破解wifi密码听起来很复杂,实际上也不是非常的复杂,极客君(微信公众帐号:极客峰)今天教大家如何 ...

  4. Webstorm编译TypeScript报错

    Accessors are only available when targeting ECMAscript 5 and higher. 解决办法: File Watchers 在tsc.cmd命令上 ...

  5. canvas 3D雪花效果

    <!DOCTYPE html> <html style="height: 100%;"> <head> <meta charset=&qu ...

  6. Java位向量的实现原理与巧妙应用

    Java位向量的巧实现原理与巧妙应用 1.博文介绍 本篇博文将会介绍几本的位运算含义.位向量介绍.BitSet实现原理.Java位向量的应用.拓展介绍Bloom Filter等. 2.位运算介绍 1) ...

  7. php中奖算法逻辑

    最近公司有两个活动, 一个是砸蛋活动, 另一个是转盘活动. 后台这边需要做接口进行对接,当用户在前台点击进行抽奖的时候,发送AJAX请求给后台,后台进行业务处理包括记录用户中奖信息,然后返回json格 ...

  8. eclipse如何安装插件

    eclipse安装插件以springsource-tool-suite为例 打开eclipse,找到help/About Eclipse/ 然后点击右下角图标 找到EclipsePlatform对应的 ...

  9. 老李分享:HTTP协议之协议头

    老李分享:HTTP协议之协议头   当我们打开一个网页时,浏览器要向网站服务器发送一个HTTP请求头,然后网站服务器根据HTTP请求头的内容生成当次请求的内容发送给浏览器.你明白HTTP请求头的具体含 ...

  10. ElasticSearch查询 第五篇:布尔查询

    布尔查询是最常用的组合查询,不仅将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起.当查询条件是多个表达式的组合时,布尔查询非常有用,实际上,布尔查询把多个子查询组合(combine)成 ...