CodeForces 591B
题目链接:
http://codeforces.com/problemset/problem/591/B
题意:
给你一串字符串,字符串里的字符全是a-z的小写字母,下面的m行,是字符串的交换方式, 如果输入的是 x y,则是字母x与字母y交换,且字母y与字母x也要交换
解题思路:
n和m的范围在2*10^5内,如果直接暴力肯定会超时;
所以只能采取智慧的方法
可以先定义
char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',};
将26个小写字母全都放在数组中存好
然后每输入一行交换方式,即对字符串中相关的字母进行变换
for(j=0;j<26;j++)
{
if(a[j]==b[0]) {a[j]=b[1];continue;}//x=j;//{x=j; cout<<"x="<<x<<endl;}
if(a[j]==b[1]) a[j]=b[0]; //y=j;//{y=j;cout<<",y="<<y<<endl;}
}
全部都交换完后,现在字符串a中的字符即是交换后的值,
for(i=0;i<n;i++)
printf("%c",a[str[i]-97]);
程序代码:
#include <cstdio>
#include <iostream>
using namespace std;
const int M=+;
int main()
{
int m,n,i,j;
char a[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',};
char str[M],b[];
cin>>n>>m>>str;
while(m--)
{ int x,y;
cin>>b[]>>b[];
//cout<<b[0]<<b[1];
for(j=;j<;j++)
{
if(a[j]==b[]) {a[j]=b[];continue;}//x=j;//{x=j; cout<<"x="<<x<<endl;}
if(a[j]==b[]) a[j]=b[]; //y=j;//{y=j;cout<<",y="<<y<<endl;}
}
//a[x]=b[1];//cout<<"x="<<x<<" a[x]="<<a[x]<<endl;
//a[y]=b[0];//cout<<"y="<<y<<" a[y]="<<a[y]<<endl;
}
for(i=;i<n;i++)
printf("%c",a[str[i]-]);
printf("\n");
return ;
}
CodeForces 591B的更多相关文章
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- CodeForces 591B Rebranding
水题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...
- 字符串 || CodeForces 591B Rebranding
给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少 arr数组记录每个字母最后被替换成了哪个字母 读入字符前面加一空格 scanf(" %c %c", &am ...
- Codeforces Round #327 (Div2) A~E
CodeForces 591A 题意:在距离为L的两端A,B,相向发射魔法,a(以P1的速度)-->B,A<--b(以P2的速度).假设a-->B,途中相遇,则返回到原点A<- ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- kissy
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- JS escape()、encodeURI()和encodeURIComponent()的区别
1.实例说明: var url='http://wx.jnqianle.com/content/images/冰皮月饼.jpg?name=张三丰&age=11'; console.info(w ...
- 自己动手写控件(模仿mvc htmlhelper的类)
自定义helper类,要求命名空间在 System.Web.Mvc之下,要求,静态类,静态方法,特殊生成对应html的返回字段, 传递Htmlhleper,返回特定类型 返回值是MvcHtmlStri ...
- js 表单不为空,数字长度验证
$(document).ready(function() { //聚焦第一个输入框 $("#invtype").focus(); ...
- C# div、css
目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...
- cocos2d-x 之 CCArray 源码分析
cocos2d-x 自己实现了一个数组CCArray ,下面我们来分析一下CCArray的源码 CCArray继承CCObject,所以,CCArray也具有引用计数功能和内存自动管理功能. 数组的源 ...
- modifytime是一个神奇的column name----这边文章是错的totally,因为我的实验不彻底。timestamp属性很神奇,头一个timestamp,会自动的成DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在mysql里边modifytime是一个神奇的column name,试一下. 请执行sql语句 CREATE TABLE `test_time` ( `modifytime` timestamp ...
- 读书笔记之 - javascript 设计模式 - 组合模式
组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...
- 关于点击空白关闭弹窗的js写法推荐?
$(document).mouseup(function(e){ var _con = $(' 目标区域 '); // 设置目标区域 ){ // Mark 1 some code... // 功能代码 ...
- Atom package安装失败的解决方案
cd ~/.atom/package git clone [package url] cd [package name] apm install [package name] if lack some ...