华为2019软件题

2. aa2(b)->aabb->bbaa

 abc4{C2[B3(A)]}->abcCBAAABAAACBAAABAAACBAAABAAACBAAABAAA->AAABAAABCAAABAAABCAAABAAABCAAABAAABCcba

   思路:使用栈

 1 #include <iostream>
 2 #include <stack>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string str;
 9     cin>>str;
10     stack<char> s;
11     for(int i=0;i<str.size();++i)
12     {
13 //        cout<<str[i]<<" ";
14         //&& 和 || 的逻辑
15         if(str[i]!='}' && str[i]!=']' && str[i]!=')')
16         {
17             s.push(str[i]);
18         }
19         else
20         {
21             string res;
22             //出栈
23             while(s.top()!='{' && s.top()!='[' && s.top()!='(')
24             {
25                 res +=s.top();
26                 s.pop();
27             }
28 //            cout<<res<<endl;
29 //            cout<<res<<endl;
30             s.pop();
31 //            cout<<s.top()<<endl;
32             int cnt = s.top()-'0';
33             s.pop();
34 //            cout<<cnt<<endl;
35             reverse(res.begin(), res.end());
36             string tmp = res;
37             for(int j=0; j<cnt-1; ++j)
38             {
39                 res += tmp;
40             }
41 //            cout<<res<<" "<<tmp<<endl;
42 
43             for(int j=0; j<res.size(); ++j)
44             {
45                 s.push(res[j]);
46             }
47         }
48 //        cout<<s.size()<<endl;
49 
50     }
51 //    cout<<res;
52 //    cout<<endl;
53     //输出栈中的元素
54     while(!s.empty())
55     {
56         cout<<s.top();
57         s.pop();
58     }
59     return 0;
60 }
61 
62 /*
63  * ab2(b)
64  * ab2(ab)
65  * abc4{C2[B3(A)]}
66  */

猜你喜欢

转载自www.cnblogs.com/cc111/p/10692018.html
今日推荐