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). The size of input file does not exceed 5MB.
Output
For each case, print the Beiju text on the screen.
Sample Input
This_is_a_[Beiju]_text [[]][][]Happy_Birthday_to_Tsinghua_University
Output for the Sample Input
BeijuThis_is_a__text Happy_Birthday_to_Tsinghua_University
思路:原先我用vector,华丽丽地超时了。。热泪盈眶!!!
TLE代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 100080
char str[maxn];
vector <char> s;
vector <char> :: iterator it;
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s",str)!=EOF)
{
s.clear();
int len = strlen(str);
bool flag = true;
int pos = 0;
for(int i = 0;i < len;i++)
{
if(str[i] == '[')
{
flag = false;
pos = 0;
}
else if(str[i] == ']')
{
flag = true;
pos = 0;
}
else
{
if(flag) s.push_back(str[i]);
else
{
s.insert(s.begin()+pos,str[i]);
pos++;
}
}
}
for(int i = 0;i < s.size();i++)
printf("%c",s[i]);
printf("\n");
}
return 0;
}
用List飘逸~~~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <list>
using namespace std;
#define maxn 100080
char str[maxn];
list <char> l;
list <char>::iterator it;
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s",str)!=EOF)
{
l.clear();
it = l.begin();
int len = strlen(str);
for(int i = 0;i < len;i++)
{
if(str[i] == '[')
{
it = l.begin();
}
else if(str[i] == ']')
{
it = l.end();
}
else
{
l.insert(it,str[i]);
}
}
for(it = l.begin();it != l.end();it++)
printf("%c",*it);
printf("\n");
}
return 0;
}