蓝桥杯练习系统(算法训练)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;
}
相关推荐
NeRF_er5 小时前
使用 VScode Debug加不上断点的原因
ide·vscode·编辑器
MediaTea13 小时前
Python 编辑器:Thonny
编辑器
June bug17 小时前
【软考中级·软件评测师】下午题·面向对象测试之架构考点全析:分层、分布式、微内核与事件驱动
经验分享·分布式·职场和发展·架构·学习方法·测试·软考
薰衣草23331 天前
一天两道力扣(1)
算法·leetcode·职场和发展
爱coding的橙子1 天前
每日算法刷题Day41 6.28:leetcode前缀和2道题,用时1h20min(要加快)
算法·leetcode·职场和发展
山顶望月川1 天前
并行科技MaaS平台支持文心4.5系列开源模型调用
人工智能·机器学习·编辑器
GDAL1 天前
Node.js REPL 教程
node.js·编辑器·vim
向宇it1 天前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
天真小巫2 天前
2025.6.27总结
职场和发展
切韵11 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器