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;
    
    }
  • 相关阅读:
    Vue.js $nextTick
    JS---函数名和变量名重名
    for循环中嵌套setTimeout,执行顺序和结果该如何理解?
    Rocket MQ整体简介
    ant Desgn Pro Vue 修改 title
    STS插件创建springboot项目,pom第一行报unkown错误
    c# 读取二进制文件并转换为 16 进制显示
    c# Winform 调用可执行 exe 文件
    按字节读取txt文件缓存区大小设置多少比较好?
    天翼云服务开放端口
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10842446.html
Copyright © 2011-2022 走看看