破损的键盘 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 @ 2025-01-14 15:59  Marinaco  阅读(22)  评论(0)    收藏  举报
//雪花飘落效果