一.存在的问题
包含错误的头文件:#include<sstring>
应该是 #include<sstream>
。
在循环内部,将 int 数组 a 的元素读取到一个字符串向量 p1 中。
排序 p2 向量,但没有使用括号()调用sort函数。
代码没有实现题目要求的功能,即删除假身字符串。
没有输出最终结果。
二.参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_set>
using namespace std;
// 将字符串分割成数字、小写字母、大写字母和下划线部分的函数
vector<string> split_string(const string &s) {
stringstream ss(s);
string temp;
vector<string> parts;
while (getline(ss, temp, '_')) {
parts.push_back(temp);
}
return parts;
}
// 检查两个字符串是否相等的函数
bool are_strings_equal(const vector<string> &a, const vector<string> &b) {
if (a.size() != b.size()) return false;
for (const auto &s : a) {
if (find(b.begin(), b.end(), s) == b.end()) {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<string> input_strings(n);
for (int i = 0; i < n; ++i) {
cin >> input_strings[i];
}
vector<string> real_strings;
unordered_set<string> real_strings_set;
for (const auto &s : input_strings) {
vector<string> parts = split_string(s);
string sorted_s;
for (const auto &p : parts) {
sorted_s += p + "_";
}
sort(sorted_s.begin(), sorted_s.end());
if (real_strings_set.find(sorted_s) == real_strings_set.end()) {
real_strings_set.insert(sorted_s);
real_strings.push_back(s);
}
}
cout << real_strings.size() << endl;
for (const auto &s : real_strings) {
cout << s << endl;
}
return 0;
}