Kattis -Backspace

Backspace

/problems/backspace/file/statement/en/img-0001.png
Bjarki having trouble

Shortly before the programming contest started, Bjarki decided to update his computer. He didn’t notice anything strange until he started coding in his favorite editor, Bim (Bjarki IMproved). Usually when he’s writing in an editor and presses the backspace key a single character is erased to the left. But after the update pressing that key outputs the character <. He’s tested all the editors on his machine, Bmacs, Neobim, bjedit, NoteBjad++ and Subjark Text, but they all seem to have the same problem. He doesn’t have time to search the web for a solution, and instead decides to temporarily circumvent the issue with a simple program.

Help Bjarki write a program that takes as input the string that was written in the text editor, and outputs the string as Bjarki intended to write it. You can assume that Bjarki never intended to write the character <, and that Bjarki never pressed the backspace key in an empty line.

Input

One line containing the string that was written in the text editor. The length of the string is at most 106106, and it will only contain lowercase letters from the English alphabet as well as the character <.

Output

One line containing the string as Bjarki intended to write it.

Sample Input 1Sample Output 1
a<bc<
b
Sample Input 2Sample Output 2
foss<<rritun
forritun

Sample Input 3Sample Output 3
a<a<a<aa<<

题意

遇到一个<就删除前一个字母,用栈来做,不过后来输出我还要用数组转换一下,不知道有什么能更方便点的方法

代码

#include<bits/stdc++.h>
using namespace std;
char aa[1000000];
int main(){
    stack<char> s;
    string str;
    cin>>str;
    int len=str.size();
    for(int i=0;i<len;i++){
        if(isalpha(str[i]))
        s.push(str[i]);
        else if(str[i]=='<')
        s.pop();
    }
    int cnt=0;
    while(!s.empty()){
        aa[cnt]=s.top();
        s.pop();
        cnt++;
    }
    for(int i=cnt-1;i>=0;i--){
        cout<<aa[i];
    }
    cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/zhien-aa/p/6279609.html