简单消消乐

替换输入字符串的 ‘b’ 和连续的 ‘a’ 和 ‘c’

/* 'aadbbcc' => 'aadcc'
   'abaca'   => 'aa'
   'aaabbccc' => '' 
*/

#include <iostream>
#include <string>
using namespace std;

string removebAndac(string& str) {
    
    
	if (str.empty()) {
    
    
		return str;
	}
	int len = -1;
	for (int i = 0; i < str.size(); i++) {
    
    
		if (str.at(i) != 'b') {
    
    
			if (str.at(i) == 'c' && str.at(len) == 'a') {
    
    
				len--;
				continue;
			}
			str.at(++len) = str.at(i);
		}
	}
	return str.substr(0, len + 1);
}

int main() {
    
    
	// string str("aadbbcc"); // output: aadcc
	// string str("abaca"); // output: aa
	string str("aaabbccc"); // output: ""
	cout << removebAndac(str) << endl;

	system("pause");
	return 0;
}

如有侵权,请联系删除,如有错误,欢迎大家指正,谢谢

猜你喜欢

转载自blog.csdn.net/xiao_ma_nong_last/article/details/105867286
今日推荐