蓝桥杯练习系统(算法训练)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;
}
相关推荐
吃着火锅x唱着歌8 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
小当家.10510 小时前
[LeetCode]Hot100系列.贪心总结+思想总结
算法·leetcode·职场和发展
努力学算法的蒟蒻12 小时前
day09(11.6)——leetcode面试经典150
算法·leetcode·职场和发展
摸鱼仙人~14 小时前
针对编程面试和算法题的基础书籍
算法·面试·职场和发展
我命由我1234514 小时前
Photoshop - Photoshop 工具栏(24)磁性套索工具
学习·ui·职场和发展·求职招聘·职场发展·课程设计·美工
逐步前行14 小时前
C/C++图形库_EasyX 环境配置(VSCode+MinGW )
ide·vscode·编辑器
weixin_4410036416 小时前
2025教资面试真题电子版|科目试讲+结构化真题解析|完整PDF
面试·职场和发展·pdf
程序员三藏17 小时前
Postman接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
名剑走天下17 小时前
在 VSCode 中:修改快捷键
ide·vscode·编辑器
名剑走天下17 小时前
在 VSCode 中:如何主动生成c_cpp_properties.json文件
ide·vscode·编辑器