zoukankan      html  css  js  c++  java
  • hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7282    Accepted Submission(s): 2639


    Problem Description
    A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
    You are to find all the hat’s words in a dictionary.
     
    Input
    Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
    Only one case.
     
    Output
    Your output should contain all the hat’s words, one per line, in alphabetical order.
     
    Sample Input
    a
    ahat
    hat
    hatword
    hziee
    word
     
    Sample Output
    ahat
    hatword
     
    Author
    戴帽子的
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1671 1298 1800 2846 1305 
     
      字典树,经典题
      题意
      给你若干个单词(不超过50000个),构成一个字典,输出这个字典中所有的帽子单词(Hat's word)。
      帽子单词(Hat's word):由两个已在字典中出现的单词构成。
      思路
      根据输入的单词构建字典树,在插入的最后做一个这是“单词”的标记。我是用一个bool型来标记,默认为false,不是单词;如果为true,则说明到这个位置为止是一个单词。将所有单词记录下来,然后每一个单词将它拆分成两份,分别判断这两份是否为一个“单词”(在字典中出现过的字符串),这样每一个单词需要判断len-1次。例如,ahat,需要判断a和hat,ah和at,aha和t。第一组符合两份都是单词,所以这是一个帽子单词,输出该单词。
      注意
      如果已经判断出这个单词是一个帽子单词,别忘了break退出循环,否则可能会重复输出。
      代码
     1 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 using namespace std;
     5 
     6 struct Tire{
     7     bool isw;    //是否是一个单词
     8     Tire *next[26];
     9     Tire()
    10     {
    11         isw = false;
    12         int i;
    13         for(i=0;i<26;i++)
    14             next[i] = NULL;
    15     }
    16 };
    17 Tire root;
    18 
    19 void Insert(char word[])    //将word插入到字典树中,在最后标记这是一个单词
    20 {
    21     Tire *p = &root;
    22     int i;
    23     for(i=0;word[i];i++){
    24         int t = word[i]-'a';
    25         if(p->next[t]==NULL)    //如果没有对应的节点
    26             p->next[t] = new Tire;
    27         p = p->next[t];
    28     }
    29     p->isw = true;    //标记到这为止是一个单词
    30 }
    31 
    32 bool isWord(char str[])    //判断这个字符串是否为一个单词
    33 {
    34     Tire *p = &root;
    35     int i;
    36     for(i=0;str[i];i++){
    37         int t = str[i]-'a';
    38         if(p->next[t]==NULL)    //如果没有对应的节点
    39             return false;
    40         p = p->next[t];
    41     }
    42     return p->isw;
    43 }
    44 char word[50010][101];    //字典
    45 
    46 int main()
    47 {
    48     int size=1,i,j;    //字典大小
    49 
    50     while(scanf("%s",word[size])!=EOF){    //读入字典
    51         //if(word[size][0]=='0') break;
    52         Insert(word[size++]);
    53     }
    54     size--;
    55 
    56     //检查每一个单词,判断这个单词是否为Hat's word
    57     for(i=1;i<=size;i++){
    58         for(j=1;j<strlen(word[i]);j++){
    59             char t[101],*p=word[i];
    60             strncpy(t,word[i],j);
    61             t[j]='';
    62             p+=j;
    63             if(isWord(t) && isWord(p)){    //如果这两部分都是单词,说明是Hat's word
    64                 printf("%s
    ",word[i]);
    65                 break;
    66             }
    67         }
    68     }
    69     return 0;
    70 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    STL标准函数库学习小总结
    3 种生成高强度密码的方法
    树莓派使用 OLED 屏显示图片及文字
    一个骚命令防止你的文件被误删除!
    B站,首战告捷!
    10 个提高效率的 Linux 命令别名
    Vim的三款实用插件
    如何高效回退到特定层级目录?
    如何将目录下的脚本一次性全部执行
    数据结构|数组为什么这么快?
  • 原文地址:https://www.cnblogs.com/yym2013/p/3783619.html
Copyright © 2011-2022 走看看