簡單排序 去重 掌握了幾個STL容器
阿新 • • 發佈:2018-10-31
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; }