Codeforces Round #297 (Div. 2)B Pasha and String

B. Pasha and String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from
left to right, where |s| is the length of the given string.

Pasha didn‘t like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his
string — each day he chose integer ai and reversed a
piece of string (a segment) from position ai to
position|s|?-?ai?+?1.
It is guaranteed that 2·ai?≤?|s|.

You face the following task: determine what Pasha‘s string will look like after m days.

Input

The first line of the input contains Pasha‘s string s of length from 2 to 2·105 characters,
consisting of lowercase Latin letters.

The second line contains a single integer m (1?≤?m?≤?105) — 
the number of days when Pasha changed his string.

The third line contains m space-separated elements ai (1?≤?ai; 2·ai?≤?|s|) — the
position from which Pasha started transforming the string on the i-th day.

Output

In the first line of the output print what Pasha‘s string s will look like after m days.

Sample test(s)

input

abcdef
1
2

output

aedcbf

input

vwxyz
2
2 2

output

vwxyz

input

abcdef
3
1 2 3

output

fbdcea

初看是模拟题,结果看了一下时间复杂度,模拟坑定超时。

ai和s+1-ai是对称的,所以反转偶数次的相当于没翻转,奇数次的要翻转,所以我们只要统计每个字母翻转的次数,用前缀和处理。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
char s[maxn];
int m;
int sum[maxn];
int p[maxn];
int main()
{
    int n;
    int ss;
    //freopen("in.txt","r",stdin);
   while(~scanf("%s",s)){
        scanf("%d",&m);
    ss=strlen(s)-1;
    memset(p,0,sizeof p);
    for(int i=0;i<m;i++){
        scanf("%d",&n);
        p[n-1]++;
        p[ss-n+1+1]--;
    }
    sum[0]=p[0];
    if(sum[0]%2==1)swap(s[0],s[ss]);
    for(int i=1;i<=ss/2;i++){
        sum[i]=sum[i-1]+p[i];
        if(sum[i]%2==1){
            swap(s[i],s[ss-i]);
        }
    }
    printf("%s\n",s);
   }
}
时间: 2024-12-28 20:15:45

Codeforces Round #297 (Div. 2)B Pasha and String的相关文章

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1

Codeforces Round #249 (Div. 2) B. Pasha Maximizes

看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素, 如果第i个元素比第i-1个元素小,则不交换 如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素 继续比较第i-1个元素与前一个元素,直到前一个元素大为止 交换元素次大于等于k则停止 但对测试用例 1234 3 则出现问题,如果按照上述方法得到答案为3214, 但正确答案是4321,直接将第4个元素往前交换 故在上面基础上改进得 当扫描第i个元素时,则要取出[i,i+k+1)之间的最大元素,然后将最大元素往前交换,

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13

Codeforces Round #297 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/525 算是比较简单的一场了,拖了好久现在才补 A. Vitaliy and Pie time limit per test:2 seconds memory limit per test:256 megabytes After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that sim

Codeforces Round #297 (Div. 2)E. Anya and Cubes

题目链接:http://codeforces.com/problemset/problem/525/E 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数. 目标: 任意给其中一些数变成阶乘,至多变k个. 再任意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制状压,0代表不取,1代表取阶乘,2代表直接取: 中途查找,节约空间: 代码如下: #include<cstdio> #include<cstring> #include<iostream> #i

Codeforces Round #288 (Div. 2) A. Pasha and Pixels

A. Pasha and Pixels time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha loves his phone and also putting his hair up... But the hair is now irrelevant. Pasha has installed a new game to

Codeforces Round #297 (Div. 2)

A题 题目大意: 给你一个字符串,奇数的时候是钥匙,偶数的时候是门,一把钥匙只能开对应的门,然后问你最少额外需要多少把钥匙. 分析: 用的数组记录一下就行,(注意的是先开门,再拿钥匙!开始错在这里了,决心好好学英语) #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<queue> #inc

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib