Codeforces Round #297 (Div. 2) B. Pasha and String 解题报告

11 篇文章 0 订阅
11 篇文章 1 订阅


Codeforces Round #297 (Div. 2)

 

B. Pasha and String解题报告

 

题目分类:贪心策略

 

题目描述:给一个长度为N2<N<=2*10^5)的字符串,有M(0<M<=10^5)次操作。每次操作给一个数字ai保证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 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;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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值