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 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.
Examples Input
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
Examples Output
cdcbcdcfcdc
题目链接:http://codeforces.com/problemset/problem/591/B
题意:先输入 1<m,n<200000,m为原字符串的长度,n为变换规则的次数。再input长度为m的字符串。接下来输入n对(c1 c2)变换规则:每次变换规则(上次变换后的字符串的 c1变成c2 c2变成c1);
思路一:炸了
第一个想到的肯定是循环n次,每次都对上一个字符串中的 c1 c2进行变换。
每次变换需要循环m次去查找有没有等于c1,c2 的字符。
那么这个暴力模拟的时间复杂度就为O(n*m) o.o... 题目时间限制是 2000ms 而 m n 都是最大20完的数,所以肯定要炸喽...
思路二:
总体来想,假如原来的名字就是一个字符,经过变换最终到另外一个字符。
也就是说一个字符通过一系列会到一个确定的字符,而一个字符串就好比多个单个字符排在一起而已。
所以我们只需要模拟26个英文字母 经过一系列变换 最终变成了什么就可以了。
AC代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN=+;
char c[]="abcdefghijklmnopqrstuvwxyz";
char ss[MAXN]={};
int main()
{//a 97 b 98
int n,m;
char c1,c2;
scanf("%d%d%*c%s%*c",&n,&m,ss);
for(int i=;i<m;i++)
{
scanf("%c%*c%c%*c",&c1,&c2);
for(int j=;j<;j++)
{
if(c[j]==c1)
c[j]=c2;
else if(c[j]==c2)
c[j]=c1;
}
}
for(int i=;i<n;i++)
printf("%c",c[ss[i]-]);
cout<<endl;
return ;
}
2017-03-09 22:03:35
codeforces 591B Rebranding (模拟)的更多相关文章
- CodeForces 591B Rebranding
水题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...
- 字符串 || CodeForces 591B Rebranding
给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少 arr数组记录每个字母最后被替换成了哪个字母 读入字符前面加一空格 scanf(" %c %c", &am ...
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. ...
- Codeforces 389B(十字模拟)
Fox and Cross Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- Codeforces 626B Cards(模拟+规律)
B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...
- Codeforces 631C. Report 模拟
C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces 679B. Barnicle 模拟
B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...
- CodeForces 382C【模拟】
活生生打成了大模拟... #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsig ...
- codeforces 719C (复杂模拟-四舍五入-贪心)
题目链接:http://codeforces.com/problemset/problem/719/C 题目大意: 留坑...
随机推荐
- QQ好友在线/离线,怎么测试?
即时通讯是目前internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,那么今天主要针对QQ好友在线状态/QQ群友在线状态功能出发,一起思考其中的实现原理以及我们如何去测试此功能? 当大 ...
- iOS网络编程笔记——Socket编程
一.什么是Socket通信: Socket是网络上的两个程序,通过一个双向的通信连接,实现数据的交换.这个双向连路的一端称为socket.socket通常用来实现客户方和服务方的连接.socket是T ...
- C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)),不对的地方欢迎指出与交流. 章节出自<Professional C# ...
- iOS开发之如何修改导航栏的内容
导航栏的内容由栈顶控制器的navigationItem属性决定. UINavigationItem有以下属性影响着导航栏的内容(通常在子控制器中viewDidLoad方法中调用这些方法): 左上角的返 ...
- centos mail使用外部SMTP发送邮件
1.安装mailx yum install mailx -y 安装好后,编辑配置文件 mailx -V 12.4 7/29/08 <<mailx的版本号 rpm -qc mailx /e ...
- centos6.5 源码安装 gtk 环境
解决 No package 'gtk+-2.0′ found问题方法:yum install libgnomeui-devel 执行了上面的,下面的就可以放弃了,yum 大法好 首先 yum 安装下面 ...
- Java并发编程:如何创建进程?
转载自:http://www.cnblogs.com/dolphin0520/p/3913517.html 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程 ...
- WebApp框架
我所知道的webapp开发框架,欢迎补充, Framework7包含ios和material两种主题风格并且有vue版和react版, vue发现一个vue-material, react有一款mat ...
- laravel blade $loop
laravel 5.3 blade 新增$loop变量 文档如下: 在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变 ...
- 【G】开源的分布式部署解决方案文档 - 部署Console & 控制负载均衡 & 跳转持续集成控制台
G.系列导航 [G]开源的分布式部署解决方案 - 导航 设置项目部署流程 项目类型:选择Console,这个跟功能无关,只是做项目分类,后面会有后续功能 宿主:选择Console 部署方式:选择原始, ...