Code Forces 591 B. Rebranding(简单模拟)
来源:互联网 发布:mysql数据库同步工具 编辑:程序博客网 时间:2024/05/26 19:16
Description
给出一个长度为n,全部由小写字母组成的字符串,现对其做m次操作,每次操作输入两个小写字母xi,yi,需将字符串中所有的xi变成yi,将所有的yi变成xi,输出操作后的字符串
Input
第一行为两个整数n和m分别表示字符串长度以及操作数,第二行为一长度为n的字符串,之后m行每行两个小写字母表示一次操作(1 ≤ n, m ≤ 200 000)
Output
输出m次操作后的字符串
Sample Input
6 1
police
p m
Sample Output
molice
Solution
暴力时间复杂度O(n*m)显然不行,所以可以简单模拟这个变化过程,记录这m次操作最后一次变换完成所有操作,具体实现就是将原字符串中的字母与操作后的字母做一个双射函数,f1[i]表示操作后的字母f1[i]对应原字符串中的字母i,f2为f1的反函数,f2[i]表示原字符串中的字母f2[i]对应操作后的字母i,每次操作输入x[i],y[i],只需swap(f1[xi-‘a’],f2[yi-‘a’]),至于f和f2的转化很简单,f2[f1[i]-‘a’]=i+’a’,最后输出f2[s[i]-‘a’]即可,时间复杂度O(n+m)
Code
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;#define maxn 222222int n,m;char f1[27],f2[27],s[maxn],a,b;int main(){ while(~scanf("%d%d",&n,&m)) { scanf("%s",s); for(int i=0;i<26;i++) f1[i]=i+'a'; while(m--) { scanf(" %c %c",&a,&b); swap(f1[a-'a'],f1[b-'a']); } for(int i=0;i<26;i++) f2[f1[i]-'a']=i+'a'; for(int i=0;i<n;i++) printf("%c",f2[s[i]-'a']); printf("\n"); } return 0;}
0 0
- Code Forces 591 B. Rebranding(简单模拟)
- Code Forces 567 B. Berland National Library(简单模拟)
- codeforces 591B - Rebranding(模拟)
- Code forces 699B
- coderforce 591B Rebranding
- CodeForces-591B Rebranding
- Codeforces 591 B:Rebranding
- codeforces 591B Rebranding
- cf 591B Rebranding
- CF 591B Rebranding
- Codeforces 591B Rebranding
- cordeforce 591 B. Rebranding
- Codeforces 591B Rebranding
- Codeforces 591B Rebranding
- codeforces round#237,div2,B,Rebranding(模拟)
- Code Forces 21 A(模拟)
- 【code forces】652A模拟
- code forces 404B Marathon
- Jsp语法结构
- ubuntu下源码安装bochs
- View的事件体系
- Light OJ 1078:Integer Divisibility【数学】
- 分享彩色图像拼接程序
- Code Forces 591 B. Rebranding(简单模拟)
- Rpi内核编译中的问题
- ArcGIS教程:成本路径 (空间分析)
- Python 条件语句
- raspberry pi 内核(Kernel)的交叉编译
- MySQL 安装和配置
- Java通过C++调用 C# DLL
- android .mk文件的用法和基础讲义
- 线程