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. 基于AGS JS开发自定义贴图图层

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.前言 假设一个景区有多张图片需要在地图上展示,并且随着地图的缩放而缩 ...

  2. Linux-ubuntu安装过程讲解

    前言也不准备介绍Linux是什么,为什么要安装ubuntu?相信你能够看到这篇文章也知道自己想要做什么. 一,准备工具 1.VMwareWorkstation虚拟机 下载地址:https://my.v ...

  3. Swift应用案例 2.闭包入门到精通

      本文主要介绍Swift的闭包的使用并与OC的Block做比较.学习Swift是绕不过闭包的,因为无论是全局函数还是嵌套函数都是闭包的一种,本文主要介绍闭包表达式. 1.闭包表达式的使用 // 1. ...

  4. Tomcat 优化和性能监测

    1. JVM 优化(Tomcat 启动行参数) Linux 修改 catalin.sh Windows 修改 catalin.bat   Linux系统中tomcat的启动参数 export JAVA ...

  5. httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接

    PoolingHttpClientConnectionManager是一个HttpClientConnection的连接池,可以为多线程提供并发请求服务.主要作用就是分配连接,回收连接等.同一个rou ...

  6. vue的使用总结

    1.vue的生命周期

  7. MCMC(四)Gibbs采样

    MCMC(一)蒙特卡罗方法 MCMC(二)马尔科夫链 MCMC(三)MCMC采样和M-H采样 MCMC(四)Gibbs采样 在MCMC(三)MCMC采样和M-H采样中,我们讲到了M-H采样已经可以很好 ...

  8. C#中ListView易错的方法

    现在有一个ListView(lv1),有2列. ListViewItem lvi = new ListViewItem(); lvi.Text = "语文"; lvi.SubIte ...

  9. div的优缺点

    div+css优缺点   产生背景 HTML语言自HTML4.01以来,不再发布新版本,原因就在于HTML语言正变得越来越复杂化.专用化.即标记越来越多,甚至各个浏览器生产商也开发出只适合于其特定浏览 ...

  10. 多线程CountDownLatch和Join

    如果现在有五个线程A.B.C.D.E,请问如何用E线程用于统计A.B.C.D四个线程的结果? 题意需要用E线程统计A.B.C.D四个线程,也就是说E线程必须要等到前面四个线程运行结束之后才能执行.那么 ...