蓝桥杯练习系统(算法训练)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;
}
相关推荐
YangYang9YangYan2 小时前
2027数学与应用数学转产品经理:面试解释与项目准备
面试·职场和发展·产品经理
java_nnnn4 小时前
LeetCode刷题双指针(有效三角形的个数+三数之和)Java
算法·leetcode·职场和发展
不要打扰7565 小时前
《房地产经纪操作实务》重点和难点是什么?用哪个题库好
经验分享·职场和发展
小李不困还能学8 小时前
VSCode远程开发免密码登录
ide·vscode·编辑器
Nil2088 小时前
leetcode 416分割等和子集
算法·leetcode·职场和发展
Pniubi9 小时前
Agent开发:记忆系统中向量存储的合理安排
职场和发展
?? Daisy9 小时前
Codex 接入第三方 API:CLI 与编辑器扩展的配置方法(2026-10)
编辑器
CoderYanger14 小时前
A.每日一题:856. 括号的分数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
bksczm1 天前
0基础面试3
面试·职场和发展
Nil2081 天前
leetcode 322零钱兑换
算法·leetcode·职场和发展