AtCoder Beginner Contest 158 D String Formation 首尾顺序可变的字符串

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;
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值