文章
34
粉丝
0
获赞
6
访问
984
1.正则表达式解法
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
// 使用 icase 标志实现大小写不敏感
regex pattern("gzu", regex_constants::icase);
string result = regex_replace(s, pattern, "");
cout << result;
return 0;
}
2.常规解法
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
string res = "";
string t = "gzu";
for(int i = 0; i < s.length(); ){
bool match = true;
for(int j = 0; j < t.length(); j ++){
// 如果匹配越界或匹配不对应,则匹配失败
if(i + j >= s.length() || tolower(s[i+j]) != t[j]){
match = false; break;
}
}
// 如果匹配成功
if(match){
i += 3;
}
// 如果匹配不成功
else {
res += s[i ++];
}
}
cout << res;
return 0;
}
登录后发布评论
暂无评论,来抢沙发