IP聚合
Accepts: 1783
Submissions: 4690
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
之前题目理解错了,以为是要输出同子网掩码下在同一网段的IP个数。
实际上是统计那n个地址在那个子网掩码下有多少个不同的网段。
#include <iostream> #include <string> #include <vector> #include <map> using namespace std; vector<string> str_vec(string a) { vector<string> res; string tmp; for (int i = 0; i < a.size(); i++) { if (a[i] == '.') { res.push_back(tmp); tmp.clear(); continue; } if (i == a.size() - 1) { tmp = tmp + a[i]; res.push_back(tmp); break; } tmp = tmp + a[i]; } return res; } vector<int> str_int(vector<string> a) { vector<int> ans; for (int i = 0; i < a.size(); i++) { string tmp = a[i]; int len = a[i].size(); int res = 0; for (int j = 0; j < len; j++) { res = res * 10 + (a[i][j] - '0'); } ans.push_back(res); } return ans; } vector<int> net_code(vector<int> a,vector<int> b) { vector<int>ans; for (int i = 0; i < 4; i++) { int tmp = a[i] & b[i]; ans.push_back(tmp); } return ans; } int main() { int t; cin >> t; for(int tt = 1; tt <= t;tt++) { int n,m; cin >> n >> m; string s; vector<vector<int> > nn; vector<vector<int> > mm; vector<string> s1; vector<int>s2; for (int i = 0; i < n; i++) { cin >> s; s1 = str_vec(s); s2 = str_int(s1); nn.push_back(s2); } for (int i = 0; i < m; i++) { cin >> s; s1 = str_vec(s); s2 = str_int(s1); mm.push_back(s2); } cout << "Case #" << tt << ":" << endl; for (int i = 0; i < m; i++) { map<vector<int>,int>mp; for (int j = 0; j < n; j++) { mp[net_code(mm[i],nn[j])]++; } cout << mp.size() << endl; } } //system("pause"); return 0; }