标签:
Description
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 Input
abcdef
1
2
aedcbf
vwxyz
2
2 2
vwxyz
abcdef
3
1 2 3
fbdcea
题意是说一个字符串,进行m次颠倒变换(从a[i]位置到a[l-i+1]位置),问得到的字符串。
容易发现,对于越在里边(对称,也就是越靠近中间位置)的字符,调换的次数越多。
我们可以把a[i]从小到大排序。
然后经过分析发现,把两个相邻的a[i]分为一组,做处理,如果m为奇数,最后还剩下a[m]没有被分组,要单独处理a[m]
细节上要注意st数组是从st[0]开始的...好吧的确不方便,适牛也说我了。。数组下标以后还是从0开始吧。。。主要是受高中OI用的pascal的影响。。。那个数组下标随便啊。
代码:
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> using namespace std; int m,k,len; const int N=2E5+7; int a[N]; char st[N]; int main() { cin>>st; scanf("%d",&m); for ( int i = 1 ; i <= m ; i++ ) scanf("%d",&a[i]); sort(a+1,a+m+1); k = 1; len = strlen(st); while (k<=m) { for ( int j = a[k] ; j <= a[k+1]-1 ; j++) swap(st[j-1],st[len-j]); k = k + 2; } if ( m %2==1 ) for ( int i = a[m]; i <= len/2 ; i++ ) swap(st[i-1],st[len-i]); cout<<st<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/111qqz/p/4403449.html