Shopee手撕代之删除字符串中所有abc组合

Shopee手撕代之删除字符串中所有abc组合

#include <bits/stdc++.h>
using namespace std;
set<string> st{"ABC","ABc","Abc","AbC","aBC","aBc","abc","abC"};
string deteteString(string str){
    string res="";
    int i=0;
    for(;i<str.size()-2;){
        string s=str.substr(i,3);
        if(st.find(s)!=st.end()){
            i+=3;
        }else{
            string s1=res[res.size()-1]+str.substr(i,2);
            if(st.find(s1)!=st.end()){
                res.erase(res.end()-1);
                i+=2;
            }else
                res+=str[i++];
        }
    }
    string s=res[res.size()-1]+str.substr(i,2);
    if(st.find(s)!=st.end()) {
        res.erase(res.end()-1);
        return res;
    }
    else {
        res+=str.substr(i,2);
        }
    return res;
}


int main() {
    //int a;
    //cin >> a;
    string s;
    cin>>s;
    string ans=deteteString(s);
    cout << ans << endl;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页