zoukankan      html  css  js  c++  java
  • HDU 1247 Hat’s Words

    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

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 typedef struct Trie
     5 {
     6     int num;
     7     bool excist;
     8     struct Trie *next[26];
     9 }Node,*trie;
    10 char ss[500000][15];
    11 Node *create()
    12 {
    13     Node *node=(Node *)malloc(sizeof(Node));
    14     node->num=0;
    15     node->excist=false;
    16     memset(node->next,0,sizeof(node->next));
    17     return node;
    18 }
    19 void insert_(trie root,char *str)
    20 {
    21     trie node=root;
    22     char *p=str;
    23     int id;
    24     while(*p)
    25     {
    26         id=*p-'a';
    27         if(node->next[id]==NULL)
    28         {
    29             node->next[id]=create();
    30         }
    31         node=node->next[id];
    32         ++p;
    33         node->num+=1;
    34     }
    35     node->excist=true;
    36 }
    37  int finds(trie root,char *str)
    38 {
    39     trie node=root;
    40     char *p=str;
    41     int id;
    42     while(*p)
    43     {
    44         id=*p-'a';
    45         node=node->next[id];
    46         ++p;
    47         if(node==NULL)
    48             return 0;
    49     }
    50     return node->excist;
    51 }
    52 int main()
    53 {
    54     trie root=create();
    55     char str[15];
    56     int k=0;
    57     while(scanf("%s",str)>0)
    58     {
    59         //if(strlen(str)==0)
    60             //break;
    61         strcpy(ss[k++],str);
    62         insert_(root,str);
    63     }
    64     char s1[15],s2[15];
    65     for(int i=0;i<k;i++)
    66     {
    67         int len=strlen(ss[i]);
    68         for(int j=1;j<len;j++)
    69         {
    70             strcpy(s1,ss[i]);
    71             //printf("%s %s
    ",s1,ss[i]);
    72             s1[j]='';
    73             //printf("%s %s
    ",s1,ss[i]);
    74             //printf("%s
    ",s1[j]);
    75             strcpy(s2,ss[i]+j);
    76             //printf("%s %s
    ",s2,ss[i]+j);
    77             if(finds(root,s1)&&finds(root,s2))
    78             {
    79                 printf("%s
    ",ss[i]);
    80                 break;
    81             }
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    《C语言课程设计与游戏开发实践课程》67章总结
    祖玛(Zuma)
    .net 实现微信公众平台的主动推送信息
    关于ASP与C#的感悟
    不同方面高手的地址。
    ASP中关于全局页面的作用 asax文件
    学习C#,开始了我的第一个进程。
    江苏立方网络科技有限公司招聘PHP工程师
    网上看到的ArcEngine控制地图显示范围的好方法(记下)
    3DS文件结构
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899644.html
Copyright © 2011-2022 走看看