解题思路:
1. 判断ip是否合法
2. 判断掩码是否合法
3. 判断ip属于哪一类(A~E类和私有不冲突,即有ip即为A类也为私有,就各统计一次)

# -*- coding: utf-8 -*-
# 开发者    : QSheng
# 代码文件  : HJ18 识别有效的IP地址和掩码并进行分类统计.py
# 创建时间  : 2021/7/29 9:58

class Solution:

    def __judge_ip_is_legal(self, ip_list):
        """ 判断IP是否合法 """
        if len(ip_list) != 4:
            return False
        for tmp in ip_list:
            if str(tmp).isdigit():
                if 0 <= int(tmp) <= 255:
                    continue
                return False
            else:
                return False
        return True

    def __judge_ip_class(self, ip_list):
        """ 判断IP属于哪个类别 """
        ip_num_list = [int(i) for i in ip_list]
        if 1 <= ip_num_list[0] <= 126:         # 有可能是A类
            if ip_num_list[0] == 10:
                return 0, 6                    # A+私网
            return 7, 0                        # A类
        elif 128 <= ip_num_list[0] <= 191:     # 有可能是B类
            if ip_num_list[0] == 172:
                if 16 <= ip_num_list[1] <= 31:
                    return 1, 6                # B+私网
            return 7, 1                        # B类
        elif 192 <= ip_num_list[0] <= 223:     # 有可能是C类
            if ip_num_list[0] == 192 and ip_num_list[1] == 168:
                return 2, 6                    # C+私网
            return 7, 2                        # C类
        elif 224 <= ip_num_list[0] <= 239:     # D类
            return 7, 3
        elif 240 <= ip_num_list[0] <= 255:     # E类
            return 7, 4

        # 其他类别(简化计算,后续不用输出)
        return 7, 7

    def __judge_mask(self, mask_ip):
        """ 判断掩码是否有效"""
        mask_num_list = [bin(int(i))[2:].zfill(8) for i in mask_ip]
        mask_str = "".join(mask_num_list)
        mask_str_list = mask_str.split("0")
        if len(mask_str_list) == 1 or len(mask_str_list) == 33:
            return False

        last_one = -1
        for idx in range(len(mask_str_list)):
            if mask_str_list[idx] == "":
                break
            last_one = idx
        if last_one == -1 or last_one > 0:
            return False

        for idx in range(last_one+1, len(mask_str_list)):
            if mask_str_list[idx] != "":
                return False
        return True


    def solve(self, ip_list, mask_list):
        """ 分类进行统计 """
        # 初始化结果(A、B、C、D、E、5错误IP地址或错误掩码、私有IP的个数, 其他【不输出】)
        rnt = [0 for i in range(8)]

        for ip, mask_ip in zip(ip_list, mask_list):
            ip_list = ip.split(".")
            # 1. 判断IP是否合法
            if self.__judge_ip_is_legal(ip_list) is False:
                rnt[5] += 1
                continue

            # 2. 进一步判断掩码
            ip_mask_list = mask_ip.split(".")
            if self.__judge_ip_is_legal(ip_mask_list) is False:
                rnt[5] += 1
                continue
            if self.__judge_mask(ip_mask_list) is False:
                rnt[5] += 1
                continue

            # 3. 判断IP是否私有, 是否ABCDE类, 其他不列入统计
            idx_1, idx_2 = self.__judge_ip_class(ip_list)
            rnt[idx_1] += 1
            rnt[idx_2] += 1

        return " ".join([str(i) for i in rnt[:-1]])

import sys
if __name__ == '__main__':
    is_IDE = False

    if is_IDE:
        fr = open("data/HJ18.txt", "r", encoding="utf-8")
    else:
        fr = sys.stdin

    ip_list, mask_list = list(), list()
    while True:
        line = fr.readline().strip()
        if line == "":
            break
        line_list = line.split("~")
        ip_list.append(line_list[0])
        mask_list.append(line_list[1])
    print(Solution().solve(ip_list, mask_list))

    if is_IDE:
        fr.close()