1、
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.
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.
For each case, print the Beiju text on the screen.
This_is_a_[Beiju]_text [[]][][]Happy_Birthday_to_Tsinghua_University
BeijuThis_is_a__text Happy_Birthday_to_Tsinghua_University
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
2、代码:
#include<stdio.h> #include<string.h> #include<deque> #include<algorithm> using namespace std; int main() { char str[100005]; int temp; while(scanf("%s",str)!=EOF) { deque<int> q; int len=strlen(str); //找到第一个不是[]的入队 for(int i=0;i<len;i++) { if((str[i]!='[')&&(str[i]!=']')) { q.push_front(i); temp=i; break; } } //将第一个不是[]的之后的[]的后一个字符位置入队,并赋值为'\0',将整个串分成一段一段的 for(int i=temp;i<len;i++) { if(str[i]=='[') { q.push_front(i+1); str[i]='\0'; } else if(str[i]==']') { q.push_back(i+1); str[i]='\0'; } } //输出,注意str+q.front()是打印str从str+q.front()前开始的字符,q.front返回的是一个数字下标 while(!q.empty()) { printf("%s",str+q.front()); q.pop_front(); //printf("%d\n",q.front()); } printf("\n"); } }