您的位置:首页 > 产品设计 > UI/UE

UVA 11988 STL deque (双端队列||链表模拟)

2015-12-22 21:01 417 查看
You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem

with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed

(internally).

You’re not aware of this issue, since you’re focusing on the text and did not even turn on the

monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

In Chinese, we can call it Beiju. Your task is to find the Beiju text.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000

letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed

internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file

(EOF).

Output

For each case, print the Beiju text on the screen.

Sample Input

This_is_a_[Beiju]_text

[[]][][]Happy_Birthday_to_Tsinghua_University

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

题意:一个不规则的文本‘[’表示home键,‘]’表示end键,这里解释一下按下home键是将光标移动到字符首位,end光标放在末尾。(大家可以试验一下,我今天才知道这两个键的用法)。求正确的文本

题解:直接模拟就可以了,链表最好,时间复杂度低,不过我想一下可以用双端队列模拟

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
#define LL long long
string s;
string temp;
deque<string>dq;
int main()
{
#ifdef CDZSC
freopen("i.txt","r",stdin);
#endif
while(cin>>s)
{
char op=0;
temp.clear();
for(int i=0;i<s.size();i++)
{

if(s[i]=='['||s[i]==']')
{
if(op=='[')
{
dq.push_front(temp);
}
else
{
dq.push_back(temp);
}
temp.clear();
op=s[i];
}
else
temp+=s[i];
if(i==s.size()-1)
{
if(op=='[')
{
dq.push_front(temp);
}
else
{
dq.push_back(temp);
}
temp.clear();
}
}
while(!dq.empty())
{
printf("%s",dq.front().c_str());
dq.pop_front();
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: