codeforces B. Pasha and String(贪心)
题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai,
将字符序列区间为[ai,len-ai+1]进行反转。求出经过n次反转之后的序列!
/*
思路1:将区间为偶数次的直接去掉!对剩下的区间进行反转。超时了,智商上的压制...
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#define N 100005
using namespace std;
char mp[*N];
int num[N];
int cnt[N*]; void my_swap(int &a, int &b){
a^=b;
b^=a;
a^=b;
} void my_reverse(int x, int y){
for(int i=x, j=y; i<j; ++i, --j){
char tmp = mp[i];
mp[i] = mp[j];
mp[j] = tmp;
}
} int main() {
scanf("%s", mp+);
int m, mm=;
cin>>m;
for(int i=; i<m; ++i){
scanf("%d", &num[i]);
++cnt[num[i]];
} int len = strlen(mp+);
for(int i=; i<=len/; ++i){
if(cnt[num[i]] + cnt[len-num[i]+])%!=){
int x = num[i];
int begin = x, end= len-x+;
if(begin > end) my_swap(begin, end);
my_reverse(begin, end);
}
}
printf("%s\n", mp+);
}
/*
思路2:仔细分析,每一个反转的区间左右是对称的,如果[ai, len-ai+1]区间进行反转,
那么就有str[ai]与str[len-ai+1]交换,str[ai+1]与str[len-ai]交换.....
也就是ai位置发生交换,那么ai+1,ai+2...len/2也一定发生交换。如果ai位置的交换的次数
为偶数就不用交换,为奇数就进行交换!
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#define N 100005
using namespace std;
char mp[*N];
int cnt[N*];//统计每一个位置交换的次数 int main() {
scanf("%s", mp+);
int m, mm=;
scanf("%d", &m) ;
int len = strlen(mp+);
while(m--){
int x;
scanf("%d", &x);
++cnt[x], ++cnt[len-x+];
} for(int i=; i<=len/; ++i)
cnt[i] += cnt[i-];//第i个位置交换,那么第i+1,i+2..len/2个位置也一定发生交换 for(int i=; i<=len/; ++i)
if(cnt[i]%!=)//奇数位置交换
swap(mp[i], mp[len-i+]);
printf("%s\n", mp+);
}
codeforces B. Pasha and String(贪心)的更多相关文章
- codeforces B. Pasha and String
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters ...
- [ An Ac a Day ^_^ ] CodeForces 525B Pasha and String 技巧
题意就是一次次翻转字符串 然后输出最终的字符串 暴力一发O(n*m)果然超时了 因为每次翻转的的都是a-1到对称位置 所以一个位置翻转两次等于没有操作 所以只需要记录一下len/2的位置前的操作次数 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- Pasha and String(思维,技巧)
Pasha and String Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- B. Pasha and String
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
随机推荐
- SQL 常用语句
替换表中某个字段中的某些字符:(将ConfigValue列中的A值替换为B) update SysConfigParams set ConfigValue=replace(ConfigValue,'A ...
- android开发时使用游标时一定要关闭
原代码如下: places = getPlaceDatas(context, cursor); cursor.close(); 应改为: try{ places = getPlaceDatas(con ...
- tomcat启动指定项目
看一下server.xml,conf/localhost/,web.xml是否配置了其他的WEBAPP应用,但实际地址已经被移除,清空WORK目录试试 http://blog.163.com/mous ...
- Apache Shiro (一)
参考博客: http://jinnianshilongnian.iteye.com/blog/2018398 1.shiro简介 Apache shiro 是一个JAVA框架,可用于身份难和授权.sh ...
- samba server install
要求: create vnc service for win7 access it via vnc viewer. 1TB disk for this Centos PC is used as Sam ...
- 03人人都应该了解的10个 jQuery 小技巧
1 返回顶部按钮 你可以利用animate和scrollTop来实现返回顶部的动画,而不需要使用其他插件. // Back to top $('a.top').click(function () { ...
- myrocks 之数据字典
data dictionary rocksdb作为mysql的一个新的存储引擎,在存储引擎层,会维护自已的元数据信息.在innodb存储引擎中,我们通过information_schema下的INNO ...
- android precelable和Serialization序列化数据传输
一 序列化原因: 1.永久性保存对象,保存对象的字节序列到本地文件中:2.通过序列化对象在网络中传递对象:3.通过序列化在进程间传递对象. 二 至于选取哪种可参考下面的原则: 1.在使用内存的时候,P ...
- node.js问题二
看了Node.js开发指南发现routes和app.js分开的话要使用下面代码 app.use(express.router(routes)) 但是真正是使用上面代码会遇到无数的问题报错 找了资料才发 ...
- [.NET领域驱动设计实战系列]专题六:DDD实践案例:网上书店订单功能的实现
一.引言 上一专题已经为网上书店实现了购物车的功能了,在这一专题中,将继续对网上书店案例进行完善,本专题将对网上书店订单功能的实现进行介绍,现在废话不多说了,让我们来一起看看订单功能是如何实现的吧. ...