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 x=t1-‘a‘;
 7            int y=t2-‘a‘;
 8            int t=zimu[x];
 9            zimu[x]=zimu[y];
10            zimu[y]=t;
11        }

后面就根据对应的路径扔到原来的char数组里就好了

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

Output

Print the new name of the corporation.

Examples

input

6 1policep m

output

molice

input

11 6abacabadabaa bb ca de gf ab b

output

cdcbcdcfcdc

Note

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

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<set>
 7 using namespace std;
 8 char text[200005];
 9 int point[30][1000000];
10 int zimu[26];
11 int main()
12 {
13
14     int n,m;
15     cin>>n>>m;
16     cin>>text;
17     for(int i=0;i<26;i++)
18         zimu[i]=i;
19     int t;
20     for(int i=0;i<n;i++)
21     {
22            t=text[i]-‘a‘;
23            point[t][0]++;
24            int p=point[t][0];
25            point[t][p]=i;
26
27     }
28     char t1,t2;
29    while(m--)
30    {
31        cin>>t1>>t2;
32        if(t1!=t2)
33        {
34            int x=t1-‘a‘;
35            int y=t2-‘a‘;
36            int t=zimu[x];
37            zimu[x]=zimu[y];
38            zimu[y]=t;
39        }
40
41    }
42    for(int i=0;i<26;i++)
43    {
44        int t=zimu[i];
45        char mmp=i+97;
46        if(point[t][0]==0) continue;
47        for(int j=1;j<=point[t][0];j++)
48        {
49            int p=point[t][j];
50            text[p]=mmp;
51         }
52    }
53    cout<<text<<endl;
54
55     return 0;
56
57
58 }
时间: 2024-10-17 03:52:30

Rebranding(字母代换)的相关文章

魔王问题(C语言实现)

[问题描述]  有一个魔王总是使用自己的一种非常精练而又抽象的语言讲话,没有人能听得懂,但他的语言是可以逐步解释成人能听懂的语言,因为他的语言是由以下两种形式的规则由人的语言逐步抽象上去的:  (1) α -> β1β2„βm  (2)(θδ1δ2„δn)->θδnθδn-1„ θδ1θ  在这两种形式中,从左到右均表示解释.试写一个魔王语言的解释系统,把他的话解释成人能听得懂的话.  [基本要求]  用下述两条具体规则和上述规则形式(2)实现.设大写字母表示魔王语言的词汇:小写字母表示人的语

几种加密算法概述

这次面试被问到密码学,问到MD5原理,一脸懵逼,所以还是简单整理一下学过的几种加密类型比较妥··· DES 1. DES背景 DES(Data Encryption Standard)又叫数据加密标准,是NIST在联邦记录中公开征集密码体制时出现的.DES由IBM开发,主要用于非国家保密机关.在1999年AES(Advanced Encryption Standard)作为DES的替代品开始使用. 2. DES原理 DES加密算法使用64位(bit)的数据进行加密和解密,所用的密钥也是64位.密

Enigma Sim-英格玛密码机模拟器

Enigma Sim是一款Enigma(英格玛)密码机模拟软件,能够对信息进行多表代换加密.该模拟器高度还原了二战时期德国所使用的三种型号Enigma的功能和细节,转子和反射器所对应的字母代换表都参考了历史上真实的Enigma密码机数据.用户通过设定转子型号.转子起始位置.反射器型号以及插线板来设定加密密码,之后可以对输入的明文信息进行加密.解密信息的一方如果知道密码的具体设定便可以将信息还原. App Store Link: comming soon...      软件特色: ·模拟三种En

退役笔记(停更啦,攒了十几篇草稿也不想补了qwq等大学玩ACM再来写吧,否则莫非你让我更高考笔记不成????gg)

终于到尽头了呢,自己的玩笑话没有省三的蒟蒻一语成谶,可能和去年学了一周上去考的分数差不多吧,gg.天天安慰组里其他人好好写暴力省一就稳了,提醒不要死磕一个题注意long long怎么的,结果自己太想AC就炸了.总之怪自己,所以不婊出题人,感觉很好玩,逃 Day1 哇,好有趣的题目,哇,不如来AC吧(emmmmm,和今年高考数学一个心态,gg的前兆)然后开始做题 T1 ,大于AB可以,大于AB-A可以,大于AB-B可以,然后陷入(m-q)A+(n-p)B=1无法自拔,中间还忘了exgcd怎么写,现

1.密码学概念及古典密码

一.前言 信息安全面临的威胁无处不在,多为人为威胁,也就是对信息的人为攻击,人为攻击可分为被动攻击和主动攻击. 被动攻击:即为窃听,分为两类:一类是消息内容获取:另一类是业务流分析,如获取消息的格式,长度及位置等敏感信息.该攻击因为对消息没有做出变动,难以检测,所以被动攻击重点在于预防而不是检测. 主动攻击:为对数据流的改动或产生假的数据流,分为三类:①中断,对系统可用性进行攻击,如破坏管理系统等:②篡改,对系统的完整性攻击,如修改传送的消息内容等:③伪造,对系统真实性攻击,如插入伪造的消息.

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

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 com

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