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

破碎的鍵盤,之前的c語言作業,當時就沒過,然後一開始想用vector,但是這個插入不行,會T,然後用連結串列寫了一下。。吐血,不知道為什麼也會T;

會T的連結串列程式碼

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include <iomanip>
  5. #include<cmath>
  6. #include<float.h>
  7. #include<string.h>
  8. #include<algorithm>
  9. #define sf scanf
  10. #define pf printf
  11. //#define pb push_back
  12. #define mm(x,b) memset((x),(b),sizeof(x))
  13. #include<vector>
  14. #include<queue>
  15. //#include<map>
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=a;i>=n;i--)
  18. typedef long long ll;
  19. typedef long double ld;
  20. typedef double db;
  21. const ll mod=1e9+100;
  22. const db e=exp(1);
  23. using namespace std;
  24. const double pi=acos(-1.0);
  25. char a[100005];
  26. struct node
  27. {
  28. char c;
  29. node *next;
  30. };
  31. int main()
  32. {
  33. int x;
  34. while(~sf("%s",&a))
  35. {
  36. node *head,*now,*last,*p;
  37. head=new node;
  38. int temp=0;
  39. last=now=head;
  40. head->next =NULL;
  41. rep(i,0,strlen(a))
  42. {
  43. if(a[i]=='[')
  44. {
  45. now=head;
  46. temp=1;
  47. }
  48. else if(a[i]==']')
  49. {
  50. now=last;
  51. temp=0;
  52. }
  53. else
  54. {
  55. p=new node;
  56. p->c =a[i];
  57. p->next=now->next ;
  58. now->next =p;
  59. now=p;
  60. if(temp==0)
  61. last=now;
  62. while(last->next!=NULL) last=last->next;
  63. }
  64. }
  65. last->next =NULL;
  66. p=head->next;
  67. while(p!=NULL)
  68. {
  69. pf("%c",p->c);
  70. now=p;
  71. p=p->next;
  72. free(now);
  73. }
  74. pf("\n");
  75. }
  76. return 0;
  77. }

後面看到劉汝佳書上有講這題,然後照著這種方法過了p143,用陣列模擬連結串列,因為是從左到右的,所以可以只用一個NEXT陣列儲存右邊跟著的字元的下表就好了;

AC的

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include <iomanip>
  5. #include<cmath>
  6. #include<float.h>
  7. #include<string.h>
  8. #include<algorithm>
  9. #define sf scanf
  10. #define pf printf
  11. //#define pb push_back
  12. #define mm(x,b) memset((x),(b),sizeof(x))
  13. #include<vector>
  14. #include<queue>
  15. //#include<map>
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=a;i>=n;i--)
  18. typedef long long ll;
  19. typedef long double ld;
  20. typedef double db;
  21. const ll mod=1e9+100;
  22. const db e=exp(1);
  23. using namespace std;
  24. const double pi=acos(-1.0);
  25. char s[100005];
  26. int NEXT[100005],last,cur;
  27. int main()
  28. {
  29. int n,last,cur;
  30. while(sf("%s",s+1)!=EOF)
  31. {
  32. n=strlen(s+1);
  33. last=cur=0;
  34. NEXT[0]=0;
  35. for(int i=1;i<=n;i++)
  36. {
  37. if(s[i]=='[')
  38. cur=0;
  39. else if(s[i]==']')
  40. cur=last;
  41. else
  42. {
  43. NEXT[i]=NEXT[cur];
  44. NEXT[cur]=i;
  45. if(cur==last) last=i;
  46. cur=i;
  47. }
  48. }
  49. for(int i=NEXT[0];i!=0;i=NEXT[i])
  50. pf("%c",s[i]);
  51. pf("\n");
  52. }
  53. return 0;
  54. }