题解 | 字符串处理
字符串处理
https://www.nowcoder.com/practice/43c57ba5728b4e8094ca1b1b18f7c7ad
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s) { //循环输入
map<char, bool> m; // 哈希表记录该字符是否出现过,也可以用数组代替
string res = ""; //保存加“_”的结果
for (int i = 0; i < s.size(); i++) { //处理res字符串,并初始化哈希表m
res += s[i];
m[tolower(s[i])] = true; //同一字母的大小写不区分
m[toupper(s[i])] = true;
if (i == s.size() - 1) //防止越界
break;
if (isalpha(s[i]) && isdigit(s[i + 1])) //对res字符串加“_”
res += '_';
if (isalpha(s[i + 1]) && isdigit(s[i]))
res += '_';
}
cout << res << endl;
for (int i = 0; i < s.size(); i++) { //输出去重数组
if (m[s[i]]) {
cout << s[i];
m[tolower(s[i])] = false;
m[toupper(s[i])] = false;
}
}
cout << endl;
}
}

查看15道真题和解析