UVa 10391:compound Words

Source: Internet
Author: User

Topic Link:

Uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&category=24&page=show_ problem&problem=1332

zoj:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=825

Type: Hash

Original title:

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

Input

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

Output

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

Sample Input

A
alien
born
less
lien
never
nevertheless
new
Newborn

Sample Output

Alien
Newborn

The main effect of the topic:

The topic is short and concise, Love ~!

Give a list of words that are sorted by dictionary and think of them as a dictionary. Then the dictionary output all the compound words, which is the word is composed of the other two words in the dictionary.

Ideas and Summary:

It is natural to think of a way to enumerate every word in order, and then to judge whether the word is a compound, and that is the output.

The core of this question is how to determine whether a word is a compound, by the definition of compound words, you can split the word into two, enumerate all the split schemes of a word, and then judge whether the two words that are split are in the dictionary.

The amount of data on the topic is 120,000, so be sure to find a quick way to determine whether a word belongs in a collection of dictionaries.

This is the super high-speed effect of the hash table is highlighted. Just give each word a hash table mapping relationship, and then it can be almost O (1) time to determine whether a word belongs to the dictionary.

* * UVA 10391-compound Words * time:0.020s (UVA) * author:d_double/#include <iostream>    
#include <cstdio> #include <cstring> #define MAXN 120003 using namespace std;  
      
      
typedef char WORD[30];  
Word WORD[MAXN];  
const int hashsize = MAXN;  
      
int N, head[hashsize], next[hashsize];    
    inline void init_lookup_table () {n=1;  
memset (head, 0, sizeof);  
    } inline int hash (char *str) {//string hash function int seed = 131;  
    int hash=0;  
    while (*str) hash = hash * seed + (*str++);  
Return (hash & 0x7fffffff)% Hashsize;  
    int Add_hash (int s) {int h = hash (word[s]);  
    int u = head[h];  
    while (u) u = next[u];  
    Next[s] = head[h];  
    HEAD[H] = s;  
return 1;  
    int search (char *s) {int h = hash (s);  
    int u = head[h];  
        while (U) {if (strcmp (Word[u], s) ==0) return u;  
    U = next[u];  
return 0;  }
      
int main () {Word str;  
    N = 1;  
    Init_lookup_table ();  
        while (gets (Word[n])) {Add_hash (N);  
    ++n;  
            }//query for (int i=1; i<n; ++i) if (Word[i][1]) {for (int j=0; J<strlen (Word[i))-1; ++j) {  
            strcpy (str, word[i]);  
            Str[j+1] = ' the ';  
            if (search (str) && search (word[i]+j+1)) {puts (word[i]);  
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.