破损的键盘 Broken Keyboard (a.k.a. Beiju Text)

题目链接:https://www.luogu.com.cn/problem/UVA11988

题意:给定若干串文本,对于每一个字符串从头开始遍历。如果遇到[ 光标从头开始,如果遇到] 光标从末尾开始。输出处理之后的字符串

思路:

基于STL list的链表的头插法与尾插法
定义迭代器it
list 的insert方法返回插入后的迭代器位置

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;


signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	string s;
	while(cin>>s)
	{
		list<char>ans;
		auto it=ans.begin();
		for(int i=0;i<s.size();i++)
		{
			 char ch=s[i];
			 if(ch=='[')
			 {
			 it=ans.begin();	
			 }else if(ch==']')
			 {
			 	it=ans.end();
			 }else{
			 	it=ans.insert(it,ch);
			 	it++;
			 }
		}
		for(it=ans.begin();it!=ans.end();it++)
		{
			cout<<*it;
		}
		cout<<endl;
	}
	return 0;
}


posted @   Marinaco  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示