SRM575 Div2 250
レーティングが760->683と下げが止まらない。
次回頑張ります。
[250の問題文]
「与えられたint型の配列から2つの要素を入れ替えて、いくつの異なった配列を作ることができるか。」
[方針]
int型配列の要素を2つ入れ替えて文字列にしてmapで判定。
[ソースコード]
#include <string> #include <vector> #include <map> #include <sstream> #include <algorithm> using namespace std; class TheSwapsDivTwo { string toString(vector<int> num) { stringstream ss; for (int i = 0; i < num.size() - 1; i++) ss << num[i] << " "; ss << num[num.size() - 1]; return ss.str(); } public: int find(vector <int> sequence) { map<string, bool> m; int cnt = 0; for (int i = 0; i < sequence.size(); i++) { for (int j = i + 1; j < sequence.size(); j++) { vector<int> ch = sequence; swap(ch[i], ch[j]); string s = toString(ch); if (m.count(s) == 0) { cnt++; m[s] = true; } } } return cnt; } };
setを使えば簡単にできるみたい。
set<vector<int>> s;
コンテナを勉強します。