蓝桥杯练习系统(算法训练)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;
}
相关推荐
做人求其滴1 小时前
蓝桥杯C/C++省赛/国赛注意事项及运行环境配置
算法·蓝桥杯·编译器·c/c++·算法竞赛·运行环境·第十六届
yuren_xia2 小时前
Vim 编辑器的常用快捷键介绍
linux·编辑器·vim
碳烤小咸鱼3 小时前
蓝桥杯 Web 方向入门指南:从基础到实战
前端·javascript·css·蓝桥杯
向宇it3 小时前
【unity游戏开发入门到精通——动画篇】Animator反向动力学(IK)
开发语言·unity·c#·编辑器·游戏引擎
一只_程序媛4 小时前
【leetcode hot 100 300】最长递增子序列
算法·leetcode·职场和发展
我劳动我光荣4 小时前
【ESP32-microros(vscode-Platformio)】
ide·vscode·编辑器
小阳拱白菜5 小时前
蓝桥杯刷题--挖矿
算法·职场和发展·蓝桥杯
100分题库橙子6 小时前
2025年汽车加气站操作工证考试内容
经验分享·职场和发展
潇湘夜雨6976 小时前
第十四届蓝桥杯大赛软件赛国赛Python大学B组题解
python·蓝桥杯
梭七y6 小时前
【力扣hot100题】(080)爬楼梯
算法·leetcode·职场和发展