zoukankan      html  css  js  c++  java
  • Hat’s Words(字典树的运用)

    个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天,

    不得不佩服这些发明算法人的大脑。

    这题的解决方法还是从网上找到的,还好算法是自己实现得,没错!组合体只要进行分割再暴力搜索就好了,

    步骤就是根据得到的字符串建立字典树,然后一一找寻时,一次拆开,只要俩边都满足在字典树里面找的到就是我们所要找的了。

    关于字典树的建立的话,因为他是不知道子树的,所以只要判断出来不存在子树就malloc一个就好了,

    注意指针的指向和递归的奥妙,很多更新都是在递归中完成的,需要好好的体会。

    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. 

    OutputYour 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<iostream>
     4 #include <algorithm>
     5 #include <queue>
     6 #include<string>
     7 using namespace std;
     8 const int length=50;
     9 const int maxn=5005;
    10 struct  word
    11 {
    12     word *next[length];
    13     bool book;
    14 
    15 };
    16 void insertword(word *root,char *s)
    17 {
    18     if(root==NULL||*s=='')
    19         return ;
    20         word *p=root;
    21     while(*s!=''){
    22             if(p->next[*s-'a']==NULL){
    23               word *q=(word *)malloc(sizeof(word));
    24                  for(int i=0;i<length;i++)
    25                     q->next[i]=NULL;
    26                     q->book=false;
    27                  p->next[*s-'a']=q;
    28                  p=p->next[*s-'a'];
    29             }
    30             else
    31             {
    32                 p=p->next[*s-'a'];
    33 
    34             }
    35             s++;
    36     }
    37     p->book=true;
    38 
    39 }
    40 bool check(word *root,char x[],int y,int z){
    41      word *p=root;
    42      for(int i=y;i<=z;i++)
    43      {
    44          if(p->next[x[i]-'a']==NULL) return false;
    45          p=p->next[x[i]-'a'];
    46     }
    47      if(p->book==true) return true;
    48      else return false;
    49 
    50 }
    51 bool searchword(word *root, char *s)
    52 {
    53     char x[100];
    54     int i=0;
    55     while(*s!='')
    56         {
    57             x[i++]=*s;
    58             s++;
    59         }
    60         for(int j=0;j<i-1;j++)
    61         {
    62             if(check(root,x,0,j)&&check(root,x,j+1,i-1)){
    63                 return true;
    64         }
    65         }
    66         return false;
    67 
    68 }
    69 char st[50005][100];
    70 int main()
    71 {
    72    word *root=(word *)malloc(sizeof(word));
    73     root->book=false;
    74     int i;
    75     for(i=0;i<length;i++)
    76       root->next[i]=NULL;
    77       i=0;
    78       while(scanf("%s",st[i])!=EOF)
    79       {
    80           insertword(root,st[i]);
    81           i++;
    82       }
    83     for(int j=0;j<i;j++)
    84     {
    85         if(searchword(root,st[j]))
    86             cout<<st[j]<<endl;
    87     }
    88     return 0;
    89 }


  • 相关阅读:
    Python安装
    php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法
    关闭sublime自动检测更新提示
    Linux系统基本命令操作汇总
    jQuery各版本CDN
    【分享】每个 Web 开发者在 2021 年必须拥有 15 个 VSCode 扩展
    React & Redux 实战 Reminder Pro 项目 免费视频教程(5 个视频)
    Redux 入门教程(React 进阶)(20 个视频)
    Nodejs + Express + MongoDB 基础篇(17 个视频)
    轻松学 nodejs
  • 原文地址:https://www.cnblogs.com/blvt/p/7340977.html
Copyright © 2011-2022 走看看