蓝桥杯练习系统(算法训练)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;
}
相关推荐
开发者联盟league42 分钟前
vscode接入deepseek实现inline suggestions
vscode·编辑器
木卫二号Coding2 小时前
Vdit 实现所见即所得(WYSIWYG) 模式的 Markdown 编辑器
编辑器
怪兽学LLM18 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展
QQ骞19 小时前
【ECU-HIL 嵌入式车载软件测试学习路线和面试描述方式(1)】
学习·面试·职场和发展
Scabbards_19 小时前
面试Leetcode - Array
leetcode·面试·职场和发展
mm99542021 小时前
PMI三大证书:PMP、PgMP、PfMP证书对比
学习·职场和发展·项目管理·学习方法·pmp
广东帝工1 天前
桥梁智能防撞主动预警系统——架构、体系、预警机制
编辑器
A小调的码农1 天前
OPENOCD+MSYS+VSCODE:从“灯不亮“到“终于亮了“
ide·vscode·stm32·单片机·编辑器
AI的探索之旅1 天前
让 Claude Code 直接操刀画原理图和 PCB:VSCode 插件接外部 API 全流程
linux·ide·vscode·嵌入式硬件·编辑器
罗超驿1 天前
双指针算法详解:从入门到精通(Java版)
算法·leetcode·职场和发展