一、题目描述:
薯队长写了一篇笔记草稿,请你帮忙输出最后内容。
1.输入字符包括,"(" , ")" 和 "<"和其他字符。
2.其他字符表示笔记内容。
3.()之间表示注释内容,任何字符都无效。 括号保证成对出现。
4."<"表示退格, 删去前面一个笔记内容字符。括号不受"<"影响 。
输入描述:
输入一行字符串。长度<=10000.
输出描述:
输出一行字符串,表示最终的笔记内容。
输入例子1:
Corona(Trump)USA<<<Virus
输出例子1:
CoronaVirus
二、思路
采用栈来保存字符,根据条件来弹出元素,剩下的就是合法的元素。最后元素出栈覆盖原字符串,同时返回合法的元素长度,进行范围输出。
三、代码实现

#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int Q1(string &str){
    int len = str.length(),j=0;
    stack<char> strStack;
    string tempstr = "";
    int hasKuoNum = 0;
    for (int i = 0; i < len; i++)
    {
        if (str[i]=='(')
        {
            hasKuoNum +=1;
            continue;
        }
        if (str[i]==')')
        {
            hasKuoNum -=1;
            continue;
        }
        if (hasKuoNum==0)
        {
            if (str[i]!='<')
                strStack.push(str[i]);
            else
                if(!strStack.empty()) strStack.pop();
        }
    }
    while (!strStack.empty())
    {
        char tt = strStack.top();
        tempstr += tt;
        strStack.pop();
    }
    len = tempstr.length();
    for (int i = len-1; i >=0; i--)
    {
        str[j] = tempstr[i];
        j++;
    }
    return j;
}
void dealwithQ1(){
    string str;
    cin>>str;
    int len = Q1(str);
    for (int i = 0; i < len; i++)
        cout<<str[i];
    cout<<endl;
}
int main(){
    dealwithQ1();
    return 0;
}

四、实现效果
图片说明