zoukankan      html  css  js  c++  java
  • 《 字典树模板_非递归 》

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 struct tree
     8 {
     9     int count;
    10     tree *next[26];
    11 };
    12 tree *head;
    13 
    14 void Insert(char *ch)
    15 {
    16     int pp=0;
    17     tree *p;
    18     p=head;
    19     for(;;)
    20     {
    21         if(ch[pp]=='') break;
    22         if(p->next[ch[pp]-'a']!=NULL)
    23         {
    24             p=p->next[ch[pp]-'a'];
    25             p->count++;
    26         }
    27         else
    28         {
    29             tree *Q=(tree *)malloc(sizeof(tree));
    30             Q->count=1;
    31             for(int i=0;i<26;i++)
    32             {
    33                 Q->next[i]=NULL;
    34             }
    35             p->next[ch[pp]-'a']=Q;
    36             p=Q;
    37         }
    38         pp++;
    39     }
    40 }
    41 
    42 int find(char *ch)
    43 {
    44     tree *p=head;
    45     int pp=0;
    46     for(;;)
    47     {
    48         if(ch[pp]=='') return p->count;
    49         if(p->next[ch[pp]-'a']==NULL) return 0;
    50         p=p->next[ch[pp]-'a'];
    51         pp++;
    52     }
    53     return 0;
    54 }
    55 int main()
    56 {
    57    // freopen("ACM.txt","r",stdin);
    58     head=(tree *)malloc(sizeof(tree));
    59     for(int i=0;i<26;i++)
    60         head->next[i]=NULL;
    61     //char a[12],b[12];
    62     //while(gets(a)&&a[0]!='')
    63     //{
    64     //    Insert(a);
    65     //}
    66     //while(scanf("%s",b)!=EOF)
    67     //{
    68     //   cout<<find(b)<<endl;
    69     //}
    70 
    71     return 0;
    72 }
  • 相关阅读:
    mysql 修改表
    mac下安装MySQL 5.7
    win&linux下path中%%与$ 以及;与:区别,
    PATH
    转 path设置方式
    MyEclipse乱码问题
    03 最大的数据库 information_schema介绍以及sql注入第一题题解
    02.mysql数据库 基本命令
    01.Windows进入MySQL数据库
    Web web4
  • 原文地址:https://www.cnblogs.com/M-D-LUFFI/p/4014454.html
Copyright © 2011-2022 走看看