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: ...
随机推荐
- 自定义Cell需要注意的问题
使用xib创建cell时需要在设置单元格样式时使用[[NSBundel mainBundel] loadNibName:@“xib的文件名”owner:self option:nil][0]来初始化单 ...
- JPEGView——专业、免费、开源的图像浏览器
虽叫JPEGView,它不仅支持jpeg图像格式,主流的图像格式它都支持. 试一试你就知道它有多强大了.
- 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...
- [NOI2008]设计路线
题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...
- Linux设置默认启动命令行,而不是图形界面
无论是作为上线的服务器还是开发中的虚拟机来说,都是没有必要启动图形界面的,而图形界面占用的内存还是很大的.枫竹梦本文就介绍如何设置来默认启动命令行. 其实就是设置系统的启动级别. CentOS的设置 ...
- linux中获取堆栈空间大小的方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include < ...
- cocos2dx打飞机项目笔记四:Enemy类和EnemyLayer类
Enemy类没什么内容,就create和init方法,根据参数来创建不同的敌机,头文件代码如下: //飞机的类型 enum planeType {smallPlane, midPlane, bigPl ...
- iOS_数据存取(二)
本节内容目录: 一.SQLite3 二.Core Data 一.SQlite3 SQLite3是⼀款开源的嵌入式关系型数据库,可移植性好.易使用.内存开销小SQLite3是⽆类型的,意味着你可以保存任 ...
- iOS_网络编程
网络编程中有以下几种方式向服务器进行提交数据: IOS同步请求.异步请求.GET请求.POST请求 1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可 ...
- INSPIRED启示录 读书笔记 - 第11章 评估产品机会
市场需求文档 大多数的公司产品选择权是由高管.市场部门.开发团队甚至是大客户,在这种情况下公司会跳过市场需求文档或是误写成产品规范文档,回避评估产品机会 在正常情况下,应该是由业务人员会撰写一份论证产 ...