博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rebranding(字母代换)
阅读量:4481 次
发布时间:2019-06-08

本文共 3115 字,大约阅读时间需要 10 分钟。

个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时。

所以我就是将输入的字母从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 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:

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
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
>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<
<

 

转载于:https://www.cnblogs.com/blvt/p/7286719.html

你可能感兴趣的文章
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
面向对象 【抽象类】【接口】【构造函数】【静态】
查看>>
结构化方法与面向对象方法的比较
查看>>
影响各类服务器性能瓶颈的因素【转】
查看>>
Jenkins
查看>>
jboss5 启动时报HsqlException:length must be specified in type definition:VARBINARY错误
查看>>
转载:让理科生沉默,让文科生流泪的综合题
查看>>
程序员7-2007-2010
查看>>
Android分类前言
查看>>
oracle中字符串的大小比较,字符串与数字的比较和运算
查看>>
2018年东北农业大学春季校赛 wyh的矩阵
查看>>
python网络爬虫与信息提取——4.Beautiful Soup库入门
查看>>
3145 汉诺塔游戏
查看>>
ASP.NET MVC做的微信WEBAPP中调用微信JSSDK扫一扫
查看>>
iOS SHA1加密实现方法
查看>>
List基本用法
查看>>
c# 正则表达式替换字符串中常见的特殊字符
查看>>
032 Longest Valid Parentheses 最长有效括号
查看>>