Rebranding(字母代换)
个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时。
所以我就是将输入的字母从a到z的数目和路径依次保存,再建立一个book数组表示字母现在所指的字母
,一开始就直接数组转换结果超时了,有一点并查集的思想。
就比如说 a b转换,a 此时有3个,分别为1,3,5。b有2个,分别为2,6
则此时a 应该指向b的二维数组,而b就指向a的数组。
while(m--)
{
cin>>t1>>t2;
if(t1!=t2)
{
int x=t1-'a';
int y=t2-'a';
int t=zimu[x];
zimu[x]=zimu[y];
zimu[y]=t;
}
后面就根据对应的路径扔到原来的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.
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.
Print the new name of the corporation.
6 1
police
p m
molice
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
cdcbcdcfcdc
In the second sample the name of the corporation consecutively changes as follows:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
char text[];
int point[][];
int zimu[];
int main()
{ int n,m;
cin>>n>>m;
cin>>text;
for(int i=;i<;i++)
zimu[i]=i;
int t;
for(int i=;i<n;i++)
{
t=text[i]-'a';
point[t][]++;
int p=point[t][];
point[t][p]=i; }
char t1,t2;
while(m--)
{
cin>>t1>>t2;
if(t1!=t2)
{
int x=t1-'a';
int y=t2-'a';
int t=zimu[x];
zimu[x]=zimu[y];
zimu[y]=t;
} }
for(int i=;i<;i++)
{
int t=zimu[i];
char mmp=i+;
if(point[t][]==) continue;
for(int j=;j<=point[t][];j++)
{
int p=point[t][j];
text[p]=mmp;
}
}
cout<<text<<endl; return ; }
Rebranding(字母代换)的更多相关文章
- B. Rebranding
http://codeforces.com/problemset/problem/591/B B. Rebranding time limit per test 2 seconds memory ...
- cf591B Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. ...
- shell中命令代换$()与`` 、 变量代换${} 、 整数运算$(( )) 的区别
命令代换$()与`` . 变量代换${} . 整数运算$(( )) 1.$( ) 与 ` ` (反引号) 在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换用(comm ...
- Codeforces 591 B:Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 在centos7上安装ClamAV杀毒,并杀毒(centos随机英文10字母)成功
前言 上传文件的时候发现总是失败,查看top发现有个进程一直cpu占用80%以上,而且名称还是随机数.kill之后,一会儿又重新生成了.突然发现居然没有在服务端杀毒的经历.在此处补齐. 安装clama ...
- HTML5游戏源码 飞翔的字母 可自定义内容
相信大家都玩过飞翔的小鸟吧,当然,可能已经有很多人因为这个游戏砸了不少手机.吼吼. 废话不多说,回到主题,源码如下. 博客园上传空间大小有限制,没法上传了,需要打包源码的朋友们请留言邮箱地址.当然还有 ...
- .net汉字转字母
目前手上有一个需要实现:将用户输入的姓名转换成汉语拼音. 使用枚举,既麻烦又易出错,发现有一个微软拼音转换工具类ChnCharInfo.dll,在此记录下: 首先需要引入此dll, 链接: http: ...
随机推荐
- iOS系统自带分享功能
很多APP中都带有社交分享功能,通过用户的分享,让更多地人去了解和使用这个APP,目前社交分享是移动互联网应用程序推广的最重要手段之一,国内较或的分享平台有微信,IOS6后苹果集成的新浪微博,还有IO ...
- 【TopCoder】SRM152 DIV2总结
为什么平常刷的时候感觉还不错,比赛的时候只能做出来一道题=.= 250分题:大水题,根据题目规则把一个字符串翻译成数字,直接代码:GitHub 我是通过遍历一个个数出来的,看到大神的解法是把字符用‘- ...
- ubuntu: lightdm 登录root超级管理员方法
ubuntu 12.04 lts 默认是不允许root登录的, 在登录窗口只能看到普通用户和访客登录. 以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后, 修改系统配置文件需要切换到超级 ...
- CMA内存管理子系统
转:http://www.wowotech.net/memory_management/cma.html 前言 本文是近期学习CMA模块的一个学习笔记,方便日后遗忘的时候,回来查询以便迅速恢复上下文. ...
- [SCOI2003]蜘蛛难题
题目 对于当年来说似乎是神题?? 做法 对于联通注水来说,我们考虑把所有能平分到水的桶同时加高度,然后暴力判断 My complete code copy来的代码 #include <cstdi ...
- JSON.parse和JSON.stringify的作用
//JSON.parse将字符串格式json转化为json对象 var str='{"name":"lingling","age":&quo ...
- different between web api and web service
https://stackoverflow.com/questions/19336347/what-is-the-difference-between-a-web-api-and-a-web-ser ...
- 深入分析理解Tomcat体系结构
Tomcat整体结构 由上图可知Tomcat的顶层容器是Server,而且一个Tomcat对应一个Server,一个server有多个service提供服务.service包含两个重要组件:Conne ...
- QT5 QtreeWidget 实现点击item事件以及右键菜单删除item 和 重命名item
1.new 一个QTreeWidget 对象,并设置头标签,和根节点(个人程序需要) QTreeWidget* treeWidget = ui.treeWidget;//我已经在ui设计师中拖了一个Q ...
- 论文笔记 — Learning to Compare Image Patches via Convolutional Neural Networks
论文: 引入论文中的一句话来说明对比图像patches的重要性,“Comparing patches across images is probably one of the most fundame ...