zoukankan      html  css  js  c++  java
  • CROC 2016

    C. Hostname Aliases

    题目连接:

    http://www.codeforces.com/contest/644/problem/C

    Description

    There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru.

    You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://[/], where:

    — server name (consists of words and maybe some dots separating them),
    / — optional part, where consists of words separated by slashes.
    We consider two to correspond to one website if for each query to the first there will be exactly the same query to the second one and vice versa — for each query to the second there will be the same query to the first one. Take a look at the samples for further clarifications.

    Your goal is to determine the groups of server names that correspond to one website. Ignore groups consisting of the only server name.

    Please note, that according to the above definition queries http:// and http:/// are different.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of page queries. Then follow n lines each containing exactly one address. Each address is of the form http://[/], where:

    consists of lowercase English letters and dots, there are no two consecutive dots, doesn't start or finish with a dot. The length of is positive and doesn't exceed 20.
    consists of lowercase English letters, dots and slashes. There are no two consecutive slashes, doesn't start with a slash and its length doesn't exceed 20.
    Addresses are not guaranteed to be distinct.

    Output

    First print k — the number of groups of server names that correspond to one website. You should count only groups of size greater than one.

    Next k lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups and names inside any group in arbitrary order.

    Sample Input

    10
    http://abacaba.ru/test
    http://abacaba.ru/
    http://abacaba.com
    http://abacaba.com/test
    http://abacaba.de/
    http://abacaba.ru/test
    http://abacaba.de/test
    http://abacaba.com/
    http://abacaba.com/t
    http://abacaba.com/test

    Sample Output

    1
    http://abacaba.de http://abacaba.ru

    Hint

    题意

    现在给你n个网站地址,这个网站地址包括域名和他的子地址

    然后如果有两个网站的子地址都是一样的话,那么就说明这两个网站其实是一样的

    现在问你一共有多少个一样的网址,输出出来。

    题解:

    可以hash,但是这道题卡单hash哦

    其实我们可以用map乱搞一波……

    先存每个域名的子地址集

    然后再通过子地址集存每一个域名就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    map<string,vector<string> >mp;
    map<vector<string>,vector<string> >ans;
    
    int main()
    {
        int n;scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            s=s.substr(7)+'/';
            int pos = s.find_first_of('/');
            mp[s.substr(0,pos)].push_back(s.substr(pos));
        }
        for(auto &p:mp)
        {
            sort(p.second.begin(),p.second.end());
            p.second.erase(unique(p.second.begin(),p.second.end()),p.second.end());
            ans[p.second].push_back(p.first);
        }
        int tot = 0;
        for(auto &p:ans)
            if(p.second.size()>1)
                tot++;
        printf("%d
    ",tot);
        for(auto &p:ans)
        {
            if(p.second.size()>1)
            {
                for(auto &t:p.second)
                    cout<<"http://"<<t<<" ";
                cout<<endl;
            }
        }
    }
  • 相关阅读:
    01 mybatis框架整体概况(2018.7.10)-
    第一课(2018.7.10)
    JavaEE 企业级分布式高级架构师课程_汇总贴
    5-1条件运算符 & 5-2
    5-3运算符的优先级
    4-3逻辑非运算符及案例 & 4-4
    4-1逻辑与运算符介绍 & 4-2逻辑或运算符介绍
    3-3if-else条件结构 & 3-4 & 3-5
    3-2if条件结构
    3-1关系运算符
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5297513.html
Copyright © 2011-2022 走看看