Codeforces Round #297 (Div. 2)
B. Pasha and String解题报告
题目分类:贪心策略
题目描述:给一个长度为N(2<N<=2*10^5)的字符串,有M(0<M<=10^5)次操作。每次操作给一个数字ai保证2·ai ≤ |s|(|s|为所给字符串的长度),把字符串的第ai到|s| - ai + 1的那一段反转一下。输出M次操作以后的字符串。
解题思路:如果直接按照题目描述的顺序操作复杂度是O(M*N),显然超时。观察发现操作与顺序无关,这样字符串有很多多余的反转,比如同一位置反转偶数次等价于不反转。可以使用数组b[i]表示i处到|S|-i+1处需要反转多少次,奇数次就交换s[i]与s[|s|-i+1]。算法复杂度O(M+N)。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
usingnamespace std;
constint N = 200010;
char s[N],c;
int b[N];
intmain()
{
int n;
while(~scanf("%s",s))
{
scanf("%d",&n);
int len=strlen(s);
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
b[x]++;
}
b[0]= 0;
for(int i=1;i<=len/2;i++)
{
b[i]= (b[i-1]+b[i])%2;
}
for(int i=1;i<=len/2;i++)
if(b[i])
{
c = s[i-1];
s[i-1]= s[len-i];
s[len-i]= c;
}
printf("%s\n",s);
}
return0;
}
原题描述:
B.Pasha and String
timelimit per test
2seconds
memorylimit per test
256megabytes
input
standardinput
output
standardoutput
Pasha got a very beautiful strings for his birthday, the string consists oflowercase 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 sohe decided to change it. After his birthday Pasha spentm days performing the followingtransformations on his string — each day he chose integerai and reversed a piece of string (asegment) from positionai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.
You face the following task: determine whatPasha's string will look like afterm days.
Input
The first line of the input containsPasha's strings of length from 2 to 2·105 characters, consisting of lowercase Latinletters.
The second line contains a single integerm (1 ≤ m ≤ 105) — the number of days whenPasha changed his string.
The third line containsm space-separated elements ai (1 ≤ ai;2·ai ≤ |s|) — the position from which Pashastarted transforming the string on the i-th day.
Output
In thefirst line of the output print what Pasha's strings will look like after m days.
Sampletest(s)
Input
abcdef
1
2
Output
aedcbf
Input
vwxyz
2
2 2
Output
vwxyz
Input
abcdef
3
1 2 3
Output
fbdcea