蓝桥杯练习系统(算法训练)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;
}
相关推荐
空中海10 小时前
iOS 动态分析、抓包与 Frida Hook
ios·职场和发展·蓝桥杯
knight_9___11 小时前
LLM工具调用面试篇5
人工智能·python·深度学习·面试·职场和发展·llm·agent
一帘忧梦12 小时前
vscode 搭建stm32开发环境 +HAL 库
ide·vscode·编辑器
陈序缘13 小时前
AI Agent 的道与术
人工智能·职场和发展·agi
浅念-13 小时前
吃透栈:LeetCode 栈算法题全解析
数据结构·c++·算法·leetcode·职场和发展·
逻辑驱动的ken14 小时前
Java高频面试考点场景题21
java·开发语言·面试·职场和发展·求职招聘
剑神一笑14 小时前
CSS Animation Timeline 可视化动画编辑器:从关键帧到流畅动画
前端·css·编辑器
穿条秋裤到处跑1 天前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
Morwit1 天前
QML组件之间的通信方案(暴露子组件)
c++·qt·职场和发展
leoufung1 天前
LeetCode 76:Minimum Window Substring 题解与滑动窗口思维详解
算法·leetcode·职场和发展