AtCoder Beginner Contest 158 比赛人数8029 codeforces比赛结束不久,就开打了,还好状态还可以,就是网站太慢,浪费了20分钟.
AtCoder Beginner Contest 158 D String Formation 首尾顺序可变的字符串
总目录详见https://blog.csdn.net/mrcrack/article/details/104454762
在线测评地址https://atcoder.jp/contests/abc158/tasks/abc158_d
该题最耗时的算法,在于变换字串顺序,
故设置头尾标识,变换顺序时,只要变化头尾标识的位置,这样就能降低变换顺序的时间复杂度。
手工算法如下:
Input2:
a
6
2 2 a
2 1 b
1
2 2 c
1
1
Ouput2:
aabc
h头部标识,t尾部标识
处理过程如下
a hat
6
2 2 a haat
2 1 b hbaat
1 tbaah
2 2 c tcbaah
1 hcbaat
1 tcbaah
编码时注意,刚开始加入的字串放在中间位置,左侧留出200010的空间,加入字串后,右侧留出200010的空间,这样左右两边都能经得起1≤Q≤2×10^5,Q次的字符插入。
AC代码如下
#include <stdio.h>
#include <string.h>
char s[500100],in[100010],c[5];
int main(){
int len,i,h,t,b,q,cmd,f;
scanf("%s",in);
len=strlen(in);
for(i=200010;i<200010+len;i++)s[i]=in[i-200010];
h=200010-1,t=200010+len;
scanf("%d",&q);
while(q--){
scanf("%d",&cmd);
if(cmd==1)b=h,h=t,t=b;
else if(cmd==2){
scanf("%d%s",&f,c);
if(f==1){//h
if(h<t)s[h]=c[0],h--;
else s[h]=c[0],h++;
}else if(f==2){//t
if(h<t)s[t]=c[0],t++;
else s[t]=c[0],t--;
}
}
}
if(h>t)
for(i=h-1;i>=t+1;i--)printf("%c",s[i]);
else
for(i=h+1;i<=t-1;i++)printf("%c",s[i]);
printf("\n");
return 0;
}