zoukankan      html  css  js  c++  java
  • M: Mysterious Conch 字符串哈希

    Problem Description
    小明有一个神奇的海螺,你对海螺说一段字符串,海螺就会返回一个单词,有字符串里面的所有字符组成
    如告诉海螺
    “lloeh”
    海螺则会告诉你
    “hello”
    如果有多个单词对应,海螺则会输出字典序最小的那个,如果没找到输入’nothing to find’(不带引号)

    Input
    第一行一个m表示m个单词 (1≤m≤105)
    接下来m行,每行一个字符串
    第m+2行,输入一个k (1≤k≤105)
    接来下k行,每行一串字符串

    (m+k个字符串长度之和小于107,每一个字符串中的相同字母不会超过5个)

    Output
    输出k行,如果查到单词输出字典序最小的那个,否则输出’nothing to find’(不带引号)

    Sample Input
    5
    abcd
    dbca
    hello
    lloeh
    xyz
    3
    cdab
    holle
    yxz
    Sample Output
    abcd
    hello
    xyz

    好像是测试数据有问题,AC不了 :)

    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #include<string.h>
    #include<string>
    #include<vector>
    typedef unsigned long long ull;
    using namespace std;
    string s[1000005], ss[1000005];
    vector<ull>p[1000005];
    ull hs(string sss)
    {
        ull temp = 0;
        for (ull i = 0; i < sss.length(); i++)
            temp = (temp + (ull)pow(3,(sss[i] - 'a'))) % 133331;
        return temp;
    }
    int main()
    {
        ull n, m;
        cin >> n;
        ios::sync_with_stdio(false);
        for (ull i = 0; i < n; i++)
        {
            std::cin >> s[i];
            p[hs(s[i])].push_back(i);
        }
        cin >> m;
        for (ull i = 0; i < m; i++)
        {
            std::cin >> ss[i];
            ull temp = hs(ss[i]);
            if (p[temp].size()== 0)
                cout << "nothing to find" << endl;
            else
            {
                string w[10000];
                for (ull i = 0; i < p[temp].size(); i++)
                {
                    w[i] = s[p[temp][i]];
                }
                sort(w, w + p[temp].size());
                cout << w[0] << endl;
            }
        }
        return 0;
    
    }
  • 相关阅读:
    eclipse.ini
    Windows8.1硬盘安装Ubuntu14.04双系统参考教程和多硬盘的注意事项[画画]
    【HTML+CSS】(1)基本语法
    Apache Curator获得真正的
    LVM逻辑卷管理@设备、格式、摩、引导自己主动安装一个完整的章节
    这么多的技术,作为一个freshman,什么研究?
    【JUnit4.10来源分析】0导航
    难度0 大写和小写交换
    java06
    java05
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10842446.html
Copyright © 2011-2022 走看看