毫无技巧的死亡暴力
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <stdlib.h>
using namespace std;
set<int> st;
void init()
{
int num = 255;
st.insert(num);
int i = 0;
while(num){
num -= 1 << i;
st.insert(num);
i++;
}
}
bool not_ip(const vector<int> & cnt){
for(auto num : cnt){
if(num < 0 || num > 255)
return true;
}
return false;
}
bool not_mask(const vector<int> & cnt){
for(int i = 0; i < cnt.size(); i++){
int num = cnt[i];
if(st.find(num) == st.end()) return true;
if(i > 0 && num != 0 && cnt[i - 1] != 255) return true;
}
if(cnt[3] == 255) return true;
if(cnt[0] == 0 && cnt[1] == 0 && cnt[2] == 0 && cnt[3] == 0) return true;
return false;
}
void solve(string& str, vector<int> &cnt){
str.push_back('.');
int pos = 0, next = 0;
while(pos < str.length()){
next = str.find('.', pos);
if(next == pos) cnt.push_back(-1);
else cnt.push_back(atoi(str.substr(pos, next - pos).c_str()));
//cout << cnt.back() << endl;
pos = next + 1;
}
cnt.push_back(atoi(str.substr(pos).c_str()));
}
int main()
{
init();
vector<int> ans(7, 0);
string str;
while(cin >> str){
int pos = str.find('~');
if(pos == string::npos)
continue;
//1.分割ip地址和mask
string ip = str.substr(0, pos);
string mask = str.substr(pos + 1);
vector<int> vip;
vector<int> vmask;
solve(ip, vip);
solve(mask, vmask);
//2.判断ip地址和mask是否合法
if(not_ip(vip) || not_mask(vmask)){
ans[5]++;
continue;
}
//3.判断类别
if(vip[0] >= 1 && vip[0] <= 126) ans[0]++;
else if(vip[0] >= 128 && vip[0] <= 191) ans[1]++;
else if(vip[0] >= 192 && vip[0] <= 223) ans[2]++;
else if(vip[0] >= 224 && vip[0] <= 239) ans[3]++;
else if(vip[0] >= 240 && vip[0] <= 255) ans[4]++;
//4.判断是否私有
if(vip[0] == 10) ans[6]++;
else if(vip[0] == 172){
if(vip[1] >= 16 && vip[1] <= 31)
ans[6]++;
}
else if(vip[0] == 192 && vip[1] == 168){
ans[6]++;
}
}
for(auto n : ans) cout << n << " ";
cout << endl;
return 0;
} 
京公网安备 11010502036488号