http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2378
vector 先排序 再去重unique
#include <iostream>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <functional>
#include <algorithm>
using namespace std;
bool judge(char ch) {
if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '-' || ch == '_')
return true;
return false;
}
vector<string> v;
bool cmp(const string a, const string b) {
return a < b;
}
int main() {
int t;
scanf("%d", &t);
getchar();
while (t--) {
v.clear();
//ans.clear();
int n;
scanf("%d", &n);
getchar();
while (n--) {
char str[150];
gets(str);
//printf("%s\n", str);
int len = strlen(str);
//printf("%d", len);
for (int i = 0; i < len ; ++i) {
//如果有后一位 如果是首 是@且后是合法 或者 前不合法中@后合法
if (i + 1 < len && (!i && str[i] == '@' && judge(str[i + 1]) || str[i] == '@' && !judge(str[i - 1]) && judge(str[i + 1]))) {
string in = "";
in += str[i + 1];
for (int j = i + 2; j < len; ++j) {
if (judge(str[j])) {
in += str[j];
}
else
break;
}
//ans.insert(in);
v.push_back(in);
}
}
}
sort(v.begin(), v.end(), cmp);
v.erase(unique(v.begin(), v.end()), v.end());
printf("%d\n", v.size());
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << endl;
}
}
return 0;
}
map的自动排序 怎么输出key值
#include <iostream>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <functional>
using namespace std;
bool judge(char ch) {
if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '-' || ch == '_')
return true;
return false;
}
map<string, int> ma;
int main() {
int t;
scanf("%d", &t);
getchar();
while (t--) {
ma.clear();
//ans.clear();
int n;
scanf("%d", &n);
getchar();
while (n--) {
char str[150];
gets(str);
//printf("%s\n", str);
int len = strlen(str);
//printf("%d", len);
for (int i = 0; i < len ; ++i) {
//如果有后一位 如果是首 是@且后是合法 或者 前不合法中@后合法
if (i + 1 < len && (!i && str[i] == '@' && judge(str[i + 1]) || str[i] == '@' && !judge(str[i - 1]) && judge(str[i + 1]))) {
string in = "";
in += str[i + 1];
for (int j = i + 2; j < len; ++j) {
if (judge(str[j])) {
in += str[j];
}
else
break;
}
//ans.insert(in);
ma[in] = 1;
}
}
}
printf("%d\n", ma.size());
for (auto it = ma.begin(); it != ma.end(); ++it) {
cout << it->first << endl;
}
}
return 0;
}
set的去重 和排序
#include <iostream>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
using namespace std;
set<string> ans;
bool judge(char ch) {
if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '-' || ch == '_')
return true;
return false;
}
int main() {
int t;
scanf("%d", &t);
getchar();
while (t--) {
ans.clear();
int n;
scanf("%d", &n);
getchar();
while (n--) {
char str[150];
gets(str);
//printf("%s\n", str);
int len = strlen(str);
//printf("%d", len);
for (int i = 0; i < len ; ++i) {
//如果有后一位 如果是首 是@且后是合法 或者 前不合法中@后合法
if (i + 1 < len && (!i && str[i] == '@' && judge(str[i + 1]) || str[i] == '@' && !judge(str[i - 1]) && judge(str[i + 1]))) {
string in = "";
in += str[i + 1];
for (int j = i + 2; j < len; ++j) {
if (judge(str[j])) {
in += str[j];
}
else
break;
}
ans.insert(in);
}
}
}
printf("%d\n", ans.size());
for (auto it = ans.begin(); it != ans.end(); ++it) {
cout << *it << endl;
}
}
return 0;
}
本文探讨了使用C++标准库中的vector和set进行数据去重和排序的方法,包括利用unique函数去除vector中重复元素,以及通过set自动进行排序和去重的过程。同时,还介绍了如何使用map来存储和输出键值对。
2684

被折叠的 条评论
为什么被折叠?



