蓝桥杯练习系统(算法训练)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;
}
相关推荐
June bug16 小时前
【HCIA- AI(正课)】3.1.人工智能业务流程
职场和发展
颜挺锐18 小时前
如何轻松通过性能测试面试之第十一篇:如何判断应用的最大处理能力
面试·职场和发展
DeepAgent19 小时前
AI Agent 面试篇(01):AI 岗位面试到底怎么走——从网申到 Offer 的完整流程
人工智能·面试·职场和发展
MR.P_H_1 天前
CCS(Code Composer Studio)修改编码格式
编辑器
2501_938140071 天前
课堂录音AI总结工具实测:上课录音转笔记,哪款更好用?
职场和发展
顶点多余1 天前
9.15 面试问题总结(一面挂)
linux·面试·职场和发展
de之梦-御风1 天前
【未来】.NET 开发在「劳动价值重估」下的定位与走向
职场和发展·.net
找方案1 天前
AI+人力资源:AI招聘面试的兴起与争议
人工智能·面试·职场和发展
测试老哥2 天前
接口测试的测试用例应该怎么写?
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
CC数分2 天前
2026年HRBP岗位硬性要求和加分项
面试·职场和发展·数据分析