蓝桥杯练习系统(算法训练)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;
}
相关推荐
一直会游泳的小猫15 分钟前
Pascal Editor:基于 WebGPU 的开源 3D 建筑编辑器技术解析
3d·开源·编辑器
蒸汽求职40 分钟前
告别静态文档:利用 Notion 搭建“交互式”简历的降维展示策略
开发语言·缓存·面试·职场和发展·金融·notion
CodeCxil42 分钟前
基于Vue的在线Online Word文档编辑器
vue.js·编辑器·word
chalmers_151 小时前
Node.js 后端 + 简易运维岗---面试全套指南(1)
面试·职场和发展·node.js
廖圣平1 小时前
Vibe Coding Laravel 使用 ueditor 编辑器
编辑器·php·laravel
zjeweler10 小时前
“网安+护网”终极300多问题面试笔记-3共3-综合题型(最多)
笔记·网络安全·面试·职场和发展·护网行动
Hacker_Nightrain11 小时前
详解Selenium 和Playwright两大框架的不同之处
自动化测试·软件测试·selenium·测试工具·职场和发展
鹿角片ljp11 小时前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
云淡风轻__17 小时前
在 VSCode 中配置 LaTeX 环境的保姆级教程
ide·vscode·编辑器
wengqidaifeng18 小时前
第十七届蓝桥杯C/C++软件赛B组算法题讲解
c语言·c++·蓝桥杯