参考:https://blog.csdn.net/lianai911/article/details/41831645
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 const int N=1e5+10;//若在函数内开过大数组,会显示Process returned -1073741571 (0xC00000FD)错误! 7 string s; 8 vector<char> v[2*N]; 9 int main() 10 { 11 std::ios::sync_with_stdio(false);//加快cin,cout效率 12 std::cin.tie(0); 13 // freopen("btext.txt","r",stdin); 14 while (cin>>s) 15 { 16 int st=N,h=N-1,e=N+1;//st为当前光标,h为头,e为尾 17 for (int i=0;i<2*N;i++) 18 { 19 v[i].clear(); 20 } 21 for (unsigned int i=0;i<s.length();i++) 22 { 23 if (s[i]=='[') 24 { 25 st=h; 26 h--; 27 continue; 28 } 29 if (s[i]==']') 30 { 31 st=e; 32 e++; 33 continue; 34 } 35 v[st].push_back(s[i]); 36 } 37 for (int i=h;i<=e;i++) 38 { 39 for (unsigned int j=0;j<v[i].size();j++) 40 { 41 cout<<v[i][j]; 42 } 43 } 44 cout<<endl; 45 } 46 47 return 0; 48 }