题目描述 Description
有一天,你需要打一份文件,但是你的键盘坏了,上面的”home”键和”end”键会时不时地按下,而你却毫不知情,甚至你都懒得打开显示器,当你打开显示器之后,出现在你的面前的是一段悲剧的文本。
输入描述 Input Description
输入只有一行,即这份文件,这份文件只包含小写字母和’[‘以及’]’,用’[‘代替”home”键,用’]’代替”end”键。
输出描述 Output Description
你的任务是在打开显示器之前,计算出这份悲剧的文档。
样例输入 Sample Input
kdg[gek]h[itj
de[co]vs
样例输出 Sample Output
itjgekkdgh
codevs
数据范围及提示 Data Size & Hint
包含多组测试数据,直到文件结束。
字符串长度小于10000个字符。
不包含空格。
双端队列。不知道原理,大概推测出s+q.front()是代表s数组里从q.front()开始到最后所有的元素。在遇到s[i]=’\0’时也会自动停止,所以输出时是一串一串输出。
就像样例:
队列里是这样:10,4,0,8
于是输出s[10]–s[12],s[4]–s[6],s[0]–s[2],s[8],组成答案。
以上只是推测,等待确认。
网上学来的代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<deque>
using namespace std;
const int maxn=100000+10;
char s[maxn];
int main()
{
while(~scanf("%s",s))
{
deque<int>q;
int i=0;
if(s[i]=='['||s[i]==']') i++;
q.push_front(i);
while(s[i])
{
if(s[i]=='[')
{
q.push_front(i+1);
s[i]='\0';
}
else if(s[i]==']')
{
q.push_back(i+1);
s[i]='\0';
}
i++;
}
while(!q.empty())
{
printf("%s",s+q.front());
q.pop_front();
}
printf("\n");
}
return 0;
}