蓝桥杯练习系统(算法训练)ALGO-952 简易编辑器

资源限制

内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s

问题描述

你要实现一个简易文本编辑器,每个字符是一个整数,程序要完成一下操作:

P 光标左移,如果在最左端则不动

N 光标右移,如果在最右端则不动

E 删除光标左侧的一个字符

R 将光标左侧内容逆序,光标移到极左

r 将光标右侧内容逆序,光标移到极右

I x 插入x

样例输入

I 12

I 56

I 89

P

r

R

E

样例输出

89

56

12

数据规模和约定

操作数少于10000

cpp 复制代码
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
list<int> text;
list<int>::iterator cursor=text.begin();
int main(){
	int n;
	cin>>n;
	
	while(n--){
		char op;
		cin>>op;
		if(op=='P'){
			if(cursor!=text.begin()){
				cursor--;
			}
		}else if(op=='N'){
			if(cursor!=text.end()){
				cursor++;
			}
		}else if(op=='E'){
			if(cursor!=text.begin()){
				list<int>::iterator pre=--cursor;
				text.erase(pre);
			}
		}else if(op=='R'){
			reverse(text.begin(),cursor);
			cursor=text.begin();
		}else if(op=='r'){
			reverse(cursor,text.end());
			cursor=text.end();			
		}else if(op=='I'){
			int x;
			cin>>x;
			text.insert(cursor,x);//list为双向链表,插入操作将元素插入到cursor指向的位置之前,而不是之后。cursor的位置也随之变化 因此才不需要cursor++ 
		}
	}
	for(list<int>::iterator it=text.begin();it!=text.end();it++){
		cout<<*it<<endl;
	} 
	return 0;
}
相关推荐
小白菜又菜6 小时前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
前端大波7 小时前
前端面试通关包(2026版,完整版)
前端·面试·职场和发展
zhaoshuzhaoshu8 小时前
人工智能(AI)发展史:详细里程碑
人工智能·职场和发展
念越9 小时前
蓝桥杯4期模拟单元测试解析
蓝桥杯·单元测试
仟濹11 小时前
2026-04-09~10-复习计划+蓝桥杯注意的点
职场和发展·蓝桥杯
羌俊恩11 小时前
Vim modeline 命令执行漏洞(CVE-2026-34714)修复指导
linux·编辑器·vim·漏洞·cve-2026-34714
skywalker_1111 小时前
力扣hot100-5(盛最多水的容器),6(三数之和)
算法·leetcode·职场和发展
生信研究猿11 小时前
leetcode 226.翻转二叉树
算法·leetcode·职场和发展
liu****13 小时前
第十五届蓝桥杯大赛软件赛国赛C/C++大学B组
c++·算法·蓝桥杯·acm
Project_Observer13 小时前
为您的项目选择最合适的Zoho Projects自动化巧能
大数据·运维·人工智能·深度学习·机器学习·自动化·编辑器