问题 J: Unhappy Hacking I(模拟栈)

问题 J: Unhappy Hacking I

时间限制: 1 Sec   内存限制: 128 MB
提交: 177   解决: 79
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.
To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:
  The 0 key: a letter 0 will be inserted to the right of the string.
  The 1 key: a letter 1 will be inserted to the right of the string.
  The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.
Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

Constraints
1≤|s|≤10 (|s| denotes the length of s)
s consists of the letters 0, 1 and B.
The correct answer is not an empty string.

输入

The input is given from Standard Input in the following format:
s

输出

Print the string displayed in the editor in the end.

样例输入

01B0

样例输出

00

提示

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#define mod 1000000007
using namespace std;
int main()
{
    char s[100], ss[100];
    int cnt=0;
    scanf("%s", s);
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(s[i]=='B')
        {
            cnt--;
            if(cnt<0)//栈为空时无论怎样删除也只能是零
                cnt=0;
        }
        else
            ss[cnt++]=s[i];
    }
    ss[cnt]='\0';
    printf("%s\n", ss);
    return 0;
}
 
/**************************************************************
    Problem: 6455
    User: ldu_reserver201701
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:1092 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值