英文原题
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 spentm days performing the following transformations on his string — each day he chose integerai andreversed a piece of string (a segment) from positionai to position|s| - ai + 1. It is guaranteed that2·ai ≤ |s|.
You face the following task: determine what Pasha's string will look like afterm days.
Input
The first line of the input contains Pasha's strings of length from 2 to2·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 elementsai (1 ≤ ai;2·ai ≤ |s|) — the position from which Pasha started transforming the string on thei-th day.
Output
In the first line of the output print what Pasha's strings will look like after m days.
样例
Input
abcdef
1
2
Output
aedcbf
Input
vwxyz
2
2 2
Output
vwxyz
Input
abcdef
3
1 2 3
Output
fbdcea
题意不是很好理解,不知道他想干嘛,分析了一下样例才明白,就是给你一个串,里面字符标号从1到n,然后他每次给你的数字ai意味着从标号ai到n-ai+1这一段翻转一下,胫骨多次操作以后问你最后的字符串。
这题显然要知道最后每个字符经历了几次翻转,由于每次翻转某一个字符翻到的位置是一定的就是它关于字符串中点对称的地方,所以我们只关心最终翻转次数的奇偶性,奇数则好对称位置,偶数则在原位。
不过记录每个字符的翻转次数还是比较困难的,这是区间操作,每一次操作从ai到n-ai+1里的所有翻转记录都加1,这里要用到树状数组的区间操作。
核心代码:
int arr[MAXN]={0};
inline int sum(int x){int res=0;while(x)res+=arr[x],x-=lowbit(x);return res;}
inline void add(int x,int n){while(x<MAXN)arr[x]+=n,x+=lowbit(x);}
inline int update(int x,int y,int n){add(x,n);add(y+1,-n);}
这里arr[]数组存放arr[x]表示该元素与左边元素的差,这题错了一次是因为没注意数据范围是200000,开始只开了100005的就RE了。
代码:
#include <iostream>
#include<stdio.h>
using namespace std;
string str;
int m,a,n;
const int MAXN=200005;
int arr[MAXN]={0};
int lowbit(int x)
{
return x&(-x);
}
inline int sum(int x){int res=0;while(x)res+=arr[x],x-=lowbit(x);return res;}
inline void add(int x,int n){while(x<MAXN)arr[x]+=n,x+=lowbit(x);}
inline int update(int x,int y,int n){add(x,n);add(y+1,-n);}
int main()
{
cin>>str;
scanf("%d",&m);
n=str.length();
for(int i=0;i<m;i++)
{
scanf("%d",&a);
update(a,n+1-a,1);
}
for(int i=1;i<=n;i++)
{
if(sum(i)%2==0)
{
printf("%c",str[i-1]);
}else
{
printf("%c",str[n-i]);
}
}
//cout << "Hello world!" << endl;
return 0;
}