Rebranding

2017-08-04

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.

Example

Input

6 1policep m

Output

molice

Input

11 6abacabadabaa bb ca de gf ab b

Output

cdcbcdcfcdc

题目大意 : 经过m次的交换后,最初的字符串变成什么样了。

题目分析 : 这道题本来就是道水题,但是考试的时候没有好好分析它的时间复杂度,一直不敢写循环。那我们现在就来好好分析一下它的时间复杂度吧!如果用最笨的方法去做,也就是

每输入一次,我们遍历整个字符串,全部交换一遍。时间复杂度O(n*m),那我们去最坏的情况来计算,也就是n=m=2*105,n*m就是4*1010次,想也不用想在一秒内肯定会超时。那怎

么办呢?我们在仔细想想,每一次输入交换的字符,无非是两个字符的值互换,那我们为什么不可以直接得最后每个字符等于什么字符呢?比如说:a和b交换,我们先不在字符串里

进行交换,而是将两者直接交换,也就是a=b,b=a。再交换b,c,那么a=b=c,b=a不变,c=b。依次类推,得到a,b,c,d最终为什么值,问题不就解决了吗?那我们再来看看它

的时间复杂度吧!输入的时候判断,并循环26个字母(找到a,变成b,找到b,变成a),最后一次遍历字符串,O(26*m+n)26又可以忽略不计,所以O(m+n)。最坏情况也不过

4*105,怎么也不会超时了。

题目收获 :数组下标的利用。也正是这个问题一直让我很困扰,也是没有想到吧!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[300009];
char ch[30];
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    scanf("%s",s);
    for(int i=0;i<26;i++)
        ch[i]=i+‘a‘;
    for(int i=0;i<m;i++)
    {
        char a,b;
        scanf(" %c %c",&a,&b);
        for(int j=0;j<26;j++)
        {
            if(ch[j]==a)
                ch[j]=b;
            else
                if(ch[j]==b)
                    ch[j]=a;
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%c",ch[s[i]-‘a‘]);//充分利用下标,a就是1,b就是2.
    }
}
 
时间: 2024-10-20 04:40:31

Rebranding的相关文章

codeforces 591B Rebranding (模拟)

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 bra

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

Rebranding(字母代换)

个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时. 所以我就是将输入的字母从a到z的数目和路径依次保存,再建立一个book数组表示字母现在所指的字母 ,一开始就直接数组转换结果超时了,有一点并查集的思想. 就比如说 a b转换,a 此时有3个,分别为1,3,5.b有2个,分别为2,6 则此时a 应该指向b的二维数组,而b就指向a的数组. 1 while(m--) 2 { 3 cin>>t1>>t2; 4 if(t1!=t2) 5 { 6 int

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

B. Rebranding 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

CodeForces 591B Rebranding

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 th

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

ural 2032  Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 题意:给定一个三角形的三条边 (a, b, c),问是否可放在二维坐标,使得3个顶点都是整数点.若可以,输出任意一组解,否则,输出 -1. 思路:暴力枚举:以 a 为半径做第一象限的 1/4 圆, 以 b 为半径做 一.四 象限的半圆,存储整数点的解,暴力枚举 a 整数点与 b 整数点是否构成长度为 c

Codeforces Round #327 (Div. 2) B Rebranding

1 /* 2 3 自从做了DP专题就没做cf惹,然而并没有什么用哇= =dp还是没啥赶脚,cf也欠了一大堆哇,还是渣渣哦多克! 4 题意: 5 给出字符串长度和要互换的字母组数,求互换后的串. 6 把每个字母最后换成啥保存起来最后输出即可. 7 */ 8 #include<cstdio> 9 #include<algorithm> 10 #include<cstring> 11 using namespace std; 12 const int maxn=200005;

URAL 2032 - Conspiracy Theory and Rebranding【本源勾股数组】

[题意] 给出三角形的三个边长,均是10^7以内的整数,问三角形的三个角的坐标是否能均是整数,输出其中任意一个解. [题解] 一开始想的是枚举一条边的横坐标,然后通过勾股定理以及算角度求出其他点的坐标,再判断是否符合条件. 亲测TLE 直到知道了本源勾股数组的构造方法... 每个本源勾股数组(a,b,c)满足a*a+b*b=c*c,其中a为奇数,b为偶数.. 枚举s,t(1<=t<s,且它们是没有公因数的奇数) a=st b=(s*s-t*t)/2 c=(s*s+t*t)/2 因为最大数c=(

随笔—邀请赛前训—Codeforces Round #327 (Div. 2) Rebranding

题意:一个字符串,做n次变换,每次变换是把a字符与b字符全部调换.求全部调换完成后的字符串. 这道题目我一开始理解错意思了,理解成将所有a字符变成b字符,我觉得这样出貌似还更有挑战性一点..口亨 #include<cstdio> #include<cstring> #include<map> #include<iostream> using namespace std; #define MAX(x,y) (((x)>(y)) ? (x) : (y))