给出一个字符串s(仅包含小写英文字母和括号),请你按照从内层到外层的顺序,逐层反转每对匹配括号内包含的字符串,并返回最终的结果。
输入描述:输入为一行带有括号的字符串(只包含英文小写字母和左右小括号,且左右括号是成对的)。
输出描述:反转括号内字符串并输出(只有英文小写字母)。
(u(love)i) 经过内层括号翻转变成(uevoli),再经过翻转得到iloveu。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<unordered_set>
#include<stack>
using namespace std;
int main()
{
string s{"(U(LOVE)I)"};
string res;
stack<string>stk;
string word="";
for(char c:s)
{
if(c=='(')
{
stk.push(word); //U在栈中
word="";
}else if(c==')')
{
reverse(word.begin(),word.end());//恢复LOVE
word=stk.top()+word;//UEVOL//LOVEU
stk.pop();//最上边那个用过了,删除
}else
{
word+=c;