Unhappy Hacking I

问题 : Unhappy Hacking I
时间限制: 1 Sec 内存限制: 128 MB

题目描述
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.
样例输入 Copy
01B0
样例输出 Copy
00
提示
Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int index=0;
    while(s.find("B")!=s.npos){
        index=s.find('B',0);
        if(index==0){
            s=s.erase(index,1);
        }else{
            s=s.erase(index-1,2);
        }
    }
    cout <<s<< endl;
    return 0;
}

http://www.cplusplus.com/
string中 find()的应用 (rfind() 类似,只是从反向查找)

原型如下:

(1)size_t find (const string& str, size_t pos = 0) const; //查找对象–string类对象

(2)size_t find (const char* s, size_t pos = 0) const; //查找对象–字符串

(3)size_t find (const char* s, size_t pos, size_t n) const; //查找对象–字符串的前n个字符

(4)size_t find (char c, size_t pos = 0) const; //查找对象–字符

结果:找到 – 返回 第一个字符的索引

 没找到--返回   string::npos

Find()
很多语言都内置了非常方便的查找函数方便我们调用,其实C++中也有find函数:

vector中
使用find()函数需要#include,例如:

vector ar1;
ar1.push_back(“aaa”);
ar1.push_back(“bbb”);
ar1.push_back(“ccc”);
ar1.push_back(“ddd”);
if(find(ar1.begin(), ar1.end(), “bbb”) != ar1.end()){
cout<<find(ar1.begin(), ar1.end(), “bbb”)-ar1.begin()<<endl;
//看到find函数的返回结果与vector的end比较可以看出其实这是一个指针,那么如果我们想要获得索引
//那么将返回结果与begin做差即可
}
else
cout<<“NO”<<endl;
1
2
3
4
5
6
7
8
9
10
11
12
string中
我是觉得find可以使用到string中真的是非常方便,例如:

string str = “hello world”;
char ch = ‘l’;
if(str.find(ch)!=string::npos){ //查找单个字符
cout<<str.find(ch)<<endl;
}
else
cout<<“NO”<<endl;
1
2
3
4
5
6
7
我们也可以看出这样返回的结果是匹配到的字符的第一个位置,如果有多个字符都可以匹配,那么只会返回第一个了,另外,关于string:npos ,如果我们故意没有匹配到,并输出结果,可以看到string:npos其实是等于4294967295的,通常使用这个值作为没有匹配到内容的结果判定。如果我们想要获取到所有的匹配字符的位置怎么办呢?其实我们find函数是有很多方式的,我们还可以通过第二个参数来控制开始查找的位置。参考:string find()

int p = 0;
while(str.find(ch, p)!=string::npos){
p = str.find(ch, p);
cout<<p<<endl;
p = p + 1;
}
1
2
3
4
5
6
这样通过一个位置p不断的更新上一次的匹配位置,我们可以不断地更新开始位置,这样就能得到所有地匹配位置了。
同样地,string地find函数也可以匹配到字符串:

string ps;
while(cin>>ps){
    if(str.find(ps)!=string::npos){
        cout<<str.find(ps)<<endl;
    }
    else
        cout<<"NO"<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值