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

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 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 ≤ aiai ≤ |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 的区间扫一遍加一,这样梅雨哦必要,因为后面一个翻转的次数总是大于等于前面一个的翻转次数,所以只由前一个累加过来就得到翻转次数;

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

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

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<string>
 8 #include<vector>
 9 using namespace std;
10 char s[200010];
11 int m,sl=0,a[200010]={0},d[100010]={0};
12 int main()
13 {
14     memset(a,0,sizeof(a));
15     scanf("%s",s);
16     scanf("%d",&m);
17     sl=strlen(s);
18     for(int i=0;i<m;i++)
19     {
20         scanf("%d",&d[i]);
21     }
22     sort(d,d+m);
23     for(int i=0;i<m;i++)
24     {
25         a[d[i]-1]++;
26         a[sl-d[i]]++;
27     }
28     for(int i=1;i<sl/2;i++)
29     {
30         a[i]+=a[i-1];
31         a[sl-i-1]+=a[sl-i];
32     }
33     for(int i=0;i<sl;i++)
34     {
35         if(a[i]%2==0)
36             printf("%c",s[i]);
37         else
38             printf("%c",s[sl-i-1]);
39     }
40     printf("
");
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/by-1075324834/p/4371908.html