题意:给定一个长度为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(贪心)的更多相关文章

  1. codeforces B. Pasha and String

    Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters ...

  2. [ An Ac a Day ^_^ ] CodeForces 525B Pasha and String 技巧

    题意就是一次次翻转字符串 然后输出最终的字符串 暴力一发O(n*m)果然超时了 因为每次翻转的的都是a-1到对称位置 所以一个位置翻转两次等于没有操作 所以只需要记录一下len/2的位置前的操作次数 ...

  3. 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 ...

  4. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  5. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  6. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  7. B. Pasha and String

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  9. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

随机推荐

  1. FreeMarker如何输出特殊含义字符

    $.#.{.}这几个字符在FreeMarker中有着特殊的含义,当需要在FreeMarker中输出这几个字符时,可采取如下办法: ${r"#{foo}"}.${r"#{& ...

  2. Core Audio(一)

    Core Audio APIs core audio apis是vista之后引入的,不使用与之前的windows版本:core audio apis提供访问endpoint devices,比如耳机 ...

  3. oracle根据视图删除表

    delete from t_fwaqjcjl where zcbm in (select zcbm from view_fwaqjcjl where 1=1 )

  4. js 监听输入框输入事件兼容ie7

    $(element).bind("input propertychange",function(){});

  5. Python成长笔记 - 基础篇 (七)python面向对象

      三大特性: 1.封装:在类中对数据赋值.内部调用对外部用户是透明的,这使类变成了一个胶囊或容器,里面包含着类的数据和方法 2.继承:一个类可以派生出子类,在父类中定义的属性.方法会自动被子类继承 ...

  6. centos 6.X minimal 系列最小化安装完成后,安装mono和jexus过程小记录

    在使用虚拟机安装minimal版centos运行mono+jexus的时候,遇到了一些坑,记录一下,比如虚拟机访问不了网络,没wget命令没开放80端口,等等小问题,其他网上教程已经有mono+jex ...

  7. 道路翻新 (Revamping Trails, USACO 2009 Feb)

    题意:给定m<=50000的1-n有联通的图,求最多可以使K<=20条边变为0的情况下的最短路是多少.. 思路:简单的分层图最短路,对于每个点拆成K个点.. 然后求一边最短路.. code ...

  8. week 2 日志

    周二 css知多少(3)——样式来源与层叠规则 http://www.cnblogs.com/wangfupeng1988/p/4277959.htmlcss知多少(4)——解读浏览器默认样式 htt ...

  9. windows 配置 apache + php

    需要工具 apache  下载地址 php for windows  (与apache同版本,使用线程安全)下载地址 vc (Visual Studio C++) 根据 apache 与 php 的版 ...

  10. 一次外企QQ面试

    无忧上挂了简历,让个外企的hr约好面试,今天刚面完,整理出来给大家看看.难度不是很大,基本就是Asp.net Mvc 用到的东西,没有问数据库方面的. Part I – Frontend  1. Tr ...