zoukankan      html  css  js  c++  java
  • UVa 10391 Compound Words 字符串hash

    Problem E: Compound Words

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

    Input

    Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.

    Output

    Your output should contain all the compound words, one per line, in alphabetical order.

    Sample Input

    a
    alien
    born
    less
    lien
    never
    nevertheless
    new
    newborn
    the
    zebra
    

    Sample Output

    alien
    newborn
    ---------------


    ---------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    const int maxn=32111;
    const int maxm=11000019;
    
    int head[maxm];
    int next[maxm];
    char word[maxn][30];
    
    int hash(char *p,int prime=10000019)
    {
        unsigned int h=0,g;
        for (;*p;++p){
            h=(h<<4)+*p;
            if (g=h&0xf0000000){
                h=h^(g>>24);
                h=h^g;
            }
        }
        return h%prime;
    }
    
    void addhash(int s)
    {
        int h=hash(word[s]);
        next[s]=head[h];
        head[h]=s;
    }
    
    bool searchstr(char* str)
    {
        int h=hash(str);
        for (int i=head[h];i!=-1;i=next[i])
        {
            if (strcmp(str,word[i])==0) return true;
        }
        return false;
    }
    
    int main()
    {
        char str[30];
        int n=1;
        memset(head,-1,sizeof(head));
        while (gets(word[n]))
        {
            addhash(n);
            n++;
        }
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<strlen(word[i]);j++)
            {
                strcpy(str,word[i]);
                str[j]='\0';
                if (searchstr(str)&&searchstr(word[i]+j))
                {
                    puts(word[i]);
                    break;
                }
            }
        }
        return 0;
    }
    





  • 相关阅读:
    ORB_SLAM2_Android
    Android惯性导航定位
    基于图像特征的图像哈希算法及实现
    Locality preserving hashing for fast image search: theory and applications
    OpenNI depth深度数据的数据格式
    速度快的国外镜像国家
    wget和curl
    关于wireshark
    ios app抓包分析
    浏览器同部署了https的服务器交互的过程
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226304.html
Copyright © 2011-2022 走看看