B. Rebranding
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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
Note

In the second sample the name of the corporation consecutively changes as follows:

不能暴力搜索,暴力会TLE

将其转化为在26个英文字母表里的序号位置,通过交换位置来实现字母的交换,最终输出对应的字母

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h> using namespace std; const int N = ; char s[N]; int main()
{
char a[], b[];
int n, m, i, q[];
while(~scanf("%d%d", &n, &m))
{
scanf("%s", s);
for(i = ; i < ; i++)
q[i] = i;
while(m--)
{
scanf("%s%s", a, b);
int x = a[] - 'a';
int y = b[] - 'a';
for(i = ; i < ; i++)
{
if(q[i] == x)
q[i] = y;
else if(q[i] == y)
q[i] = x;
}
}
for(i = ; s[i] != '\0' ; i++)
printf("%c", q[s[i] - 'a'] + 'a');
printf("\n");
}
return ;
}
 

B. Rebranding的更多相关文章

  1. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

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

  2. Codeforces Round #327 (Div. 2) B. Rebranding 水题

    B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...

  3. cf591B Rebranding

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. codeforces 591B Rebranding (模拟)

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

  5. ural 2032 Conspiracy Theory and Rebranding (数学水题)

    ural 2032  Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 ...

  6. Rebranding(模拟+思维)

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

  7. Rebranding(字母代换)

    个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时. 所以我就是将输入的字母从a到z的数目和路径依次保存,再建立一个book数组表示字母现在所指的字母 ,一开始就直接 ...

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

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

  9. Codeforces 591 B:Rebranding

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. phpcms还原被删除的栏目

    1.在这个目录下/caches/bakup/default导出文件category.sql 2.登录网站的数据管理页面phpmyadmin 3.导入数据库选择category.sql 4.登陆网站后台 ...

  2. 更新Code First生成的数据库

    1,首次访问时会自动生成数据库 2,某个Model增加一个字段后,再次访问会报,数据库不是最新 操作 1,Enable-Migrations 注意选择Default project为Star.Core ...

  3. python 对数函数

    from math import logfrom math import e print e #自然对数print log(e) #log函数默认是以e为底print log(100,10) #以10 ...

  4. MYSQL BENCHMARK函数的使用

    MYSQL BENCHMARK函数是最重要的函数之一,下文对该函数的使用进行了详尽的分析,如果您对此感兴趣的话,不妨一看. 下文为您介绍的是MYSQL BENCHMARK函数的语法,及一些MYSQL  ...

  5. LR 解决中文乱码(来源——百度)

    因为我们使用的中文操作系统默认的中文编码格式是GB2312,所以LR对服务器的返回内容自动使用GB2312方式阅读的,但是几乎所有的中文网站现在都在使用UTF-8的方式来编码,由于解码编码的方式不同最 ...

  6. js保留小数点后N位的方法介绍

    js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...

  7. unity3d Hair real time rendering 真实头发实时渲染(转)

    惊现塞拉酱 算法是Weta Digital根据siggraph2003的论文加以改进,改进之前使用的是Kajiya and Kay’s 模型,它能量不守恒,也就是说不是基于物理的,不准确 电镜下真实头 ...

  8. 横版动作MOBA《超宇宙》首测试玩曝光 详解游戏特色(转)

    http://play.163.com/15/0911/11/B37RHHO100314J6L.html

  9. SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 使用SpringMVC ...

  10. Hybrid App简介

    Hybrid App 是混合模式应用的简称,兼具 Native App 和 Web App 两种模式应用的优势,开发成本低,拥有Web技术跨平台特性.目前大家所知道的基于中间件的移动开发框架都是采用的 ...