#include <iostream>
#include <string>
using namespace std;
string removeK(string str, int k)
{
if(k < 1)
return str;
int count = 0;
int start = -1;
for(int i = 0; i < str.size(); ++i)
if(str[i] == '0')
{
++count;
start = start == -1 ? i : start;
} else {
if(count == k)
str.erase(str.begin() + start, str.begin() + start + count);
count = 0;
start = -1;
}
if(count == k)
str.erase(str.begin() + start, str.begin() + start + count);
return str;
}
int main()
{
string str = "A00B000";
cout << removeK(str, 2) << endl;
}
去掉字符串中連續出現K個0的子串
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.