您的位置:首页 > 其它

cf- 297 < b > -- 区间翻转操作的优化

2015-03-27 15:16 99 查看
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

*****************************************************

题目简述:

给一串字符,给m步操作,每步操作是将从字符串的第n个开始到第 lenth-n+1 的区间进行翻转;求最后的字符串;

*******************************************************

本题暴力,用一个数组记录该字符背反转的次数,被翻转偶数次就相当与没有翻转,基数此就相当于和对位的字符进行一次交换;

刚开始我是 n^2 的算法,从第n个到第 lenth - n -1 的区间扫一遍加一,这样梅雨哦必要,因为后面一个翻转的次数总是大于等于前面一个的翻转次数,所以只由前一个累加过来就得到翻转次数;

首先扫一遍操作数组(先排个序),将区间的首尾位置标记出来,(就是加一),区间首之间的元素翻转次数是和前一个区间首次数相同的,然后来扫一遍数组,将各个元素的翻转次数累加出来,前后是堆成的,所以是要扫一半就可以了,同时处理前后两个对应的节点;

最后输出的时候检验此数,偶数鸳鸯输出,奇数输出对位元素;

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
char s[200010];
int m,sl=0,a[200010]={0},d[100010]={0};
int main()
{
memset(a,0,sizeof(a));
scanf("%s",s);
scanf("%d",&m);
sl=strlen(s);
for(int i=0;i<m;i++)
{
scanf("%d",&d[i]);
}
sort(d,d+m);
for(int i=0;i<m;i++)
{
a[d[i]-1]++;
a[sl-d[i]]++;
}
for(int i=1;i<sl/2;i++)
{
a[i]+=a[i-1];
a[sl-i-1]+=a[sl-i];
}
for(int i=0;i<sl;i++)
{
if(a[i]%2==0)
printf("%c",s[i]);
else
printf("%c",s[sl-i-1]);
}
printf("\n");
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: