076

Zero Judge i524. 11988: Broken Keyboard (a.k.a. Beiju Text)

076
Sep 24th, 2024 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. //Solution 1
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. void solve(string &s){
  5.     bool f=true;
  6.     stack<char> a,t;
  7.     queue<char> b;
  8.     for(char &i:s){
  9.         if(i=='['){
  10.             f=false;
  11.             while(!t.empty()){
  12.                 a.emplace(t.top());
  13.                 t.pop();
  14.             }
  15.         }else if(i==']') f=true;
  16.         else if(f) b.emplace(i);
  17.         else t.emplace(i);
  18.     }
  19.     while(!t.empty()){
  20.       a.emplace(t.top());
  21.       t.pop();
  22.     }
  23.     while(!a.empty()){
  24.         cout<<a.top();
  25.         a.pop();
  26.     }
  27.     while(!b.empty()){
  28.         cout<<b.front();
  29.         b.pop();
  30.     }
  31.     cout<<'\n';
  32. }
  33. int main(){
  34.     std::ios_base::sync_with_stdio(0);
  35.     cin.tie(0);
  36.     string s;
  37.     while(cin>>s) solve(s);
  38.     return 0;
  39. }
  40. /*
  41. //Solution 2:
  42. #include<bits/stdc++.h>
  43. using namespace std;
  44. void solve(string &s){
  45.     bool f=true;
  46.     stack<string> a;
  47.     string b="",t="";
  48.     for(char &i:s){
  49.         if(i=='['){
  50.             f=false;
  51.             a.emplace(t);
  52.             t="";
  53.         }else if(i==']') f=true;
  54.         else if(f) b+=i;
  55.         else t+=i;
  56.     }
  57.     if(t!="") a.emplace(t);
  58.     while(!a.empty()){
  59.         cout<<a.top();
  60.         a.pop();
  61.     }
  62.     cout<<b<<'\n';
  63. }
  64. int main(){
  65.     std::ios_base::sync_with_stdio(0);
  66.     cin.tie(0);
  67.     string s;
  68.     while(cin>>s) solve(s);
  69.     return 0;
  70. }
  71. */
  72. /*
  73. Zero Judge i524. 11988: Broken Keyboard (a.k.a. Beiju Text)
  74. https://zerojudge.tw/ShowProblem?problemid=i524
  75. 2024 September 24
  76. Solution 1: AC (7ms, 324KB)
  77. Solution 2: AC (2ms, 348KB)
  78. */
Advertisement
Add Comment
Please, Sign In to add comment