• ECNU 3049 Hosts排序


    ECNU 3049 Hosts排序

    链接

    https://acm.ecnu.edu.cn/problem/3049

    题目

    单点时限: 2.0 sec

    内存限制: 256 MB

    在操作系统中有一个 hosts 文件,其作用是在访问互联网时将 URL(Uniform Resources Locator) 转换成 IP 地址。

    文件由若干行组成(假设最多为 1000 行),每行由两部分组成:IP 地址和 URL,之间由若干空格分隔。

    IP 地址由 4 个以 ‘.’ 分隔的十进制数(范围 0 - 255)组成,URL 可看作是一个字符串(假设字符串长度最大为 200)。

    例如:

    74.125.129.98 www.google.com

    74.125.129.98 是 www.google.com 的IP地址。

    定义结构体 PAIR 表示 hosts 文件中的一行信息。

    // * Specification of PAIR *

    typedef struct { char IP[16], URL[201]; } PAIR;

    定义函数 Sort,按 IP 地址从大到小排序(IP 地址相同时把 URL 看作字符串从小到大排序)。

    IP 地址值的比较先按照第 1 个十进制数比较,相同时再按照第 2 个十进制数比较,依次类推。

    输入格式
    第 1 行:一个整数 () 为问题数。

    对于每个问题,首先在一行中有一个整数 n (1≤T≤10),表示后面有 n 行信息。每行信息按前面描述的格式输入。

    输出格式
    对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。

    然后对应每个问题在 n 行中输出排序后的信息,IP 和 URL 之间留一个空格(除此之外没有其他空格)。

    样例
    input
    3
    1
    74.125.129.98 www.google.com
    2
    74.125.129.98 plusone.google.com
    74.125.207.19 mail.google.com
    3
    74.125.129.98 cloud.google.com
    74.125.129.98 code.l.google.com
    74.125.129.98 code.google.com
    output
    case #0:
    74.125.129.98 www.google.com
    case #1:
    74.125.207.19 mail.google.com
    74.125.129.98 plusone.google.com
    case #2:
    74.125.129.98 cloud.google.com
    74.125.129.98 code.google.com
    74.125.129.98 code.l.google.com

    思路

    排序类的问题,还是用比较器,把ip切为abcd四段进行比较,输出时可以合成,也可以记录完整的ip,如果ip相同再比较url,就是字典序比较。

    代码

     public static class host {
    
        public int a;
        public int b;
        public int c;
        public int d;
        public String url;
        public String ip;
    
        public host(String ip, String url) {
          String[] str = ip.split("\.");
          this.url = url;
          this.ip = ip;
          this.a = Integer.valueOf(str[0]);
          this.b = Integer.valueOf(str[1]);
          this.c = Integer.valueOf(str[2]);
          this.d = Integer.valueOf(str[3]);
        }
      }
    
      public static int zidian(String a, String b) {
        StringBuffer sb1 = new StringBuffer(a);
        StringBuffer sb2 = new StringBuffer(b);
        int len = Math.min(sb1.length(), sb2.length());
        for (int i = 0; i < len; i++) {
          char q = sb1.charAt(i);
          char w = sb2.charAt(i);
          if (q != w) {
            return q - w;
          }
        }
        return sb1.length() - sb2.length();
      }
    
      public static void fun() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    
        for (int i = 0; i < n; i++) {
          int sum = sc.nextInt();
          host[] h = new host[sum];
          for (int j = 0; j < sum; j++) {
            String ip = sc.next();
            String url = sc.next();
            h[j] = new host(ip, url);
          }
          Arrays.sort(h, new Comparator<host>() {
            @Override
            public int compare(host o1, host o2) {
              if (o1.a != o2.a) {
                return o2.a - o1.a;
              } else if (o1.b != o2.b) {
                return o2.b - o1.b;
              } else if (o2.c != o1.c) {
                return o2.c - o1.c;
              } else if (o1.d != o2.d) {
                return o2.d - o1.d;
              } else {
                return zidian(o1.url, o2.url);
              }
            }
          });
    
          System.out.println("case #" + i + ":");
    
          for (int j = 0; j < sum; j++) {
            System.out.println(h[j].ip + " " + h[j].url);
          }
        }
    
  • 相关阅读:
    网络编程实验一 win socket基础 获取服务器时间
    ASP.NET Core入门
    vue-element-template 获取后端路由表动态生成权限
    vue-admin-template只有一个子菜单时父级菜单不显示问题
    vue-element-template 本地使用proxy解决跨域问题
    已成功与服务器建立连接,但是在登录过程中发生错误。 (provider: SSL Provider, error: 0
    系统还未初始化,还不能审核期间以后的单据
    金蝶初始化问题
    jqueryMobile 动态添加元素,展示刷新视图方法
    链接数据库 远程事务的处理方式
  • 原文地址:https://www.cnblogs.com/blogxjc/p/14382799.html
Copyright © 2020-2023  润新知