zoukankan      html  css  js  c++  java
  • 字典树模板

     1 typedef struct Node       
     2 {
     3     Node *next[26];
     4     int cut;
     5 }Node;
     6 Node *root;
     7 void inser(char *s)
     8 {
     9     Node *p=root;
    10     for (int i=0;s[i];i++)
    11     {
    12         int x=s[i]-'a';
    13         if (p->next[x]==NULL)
    14         {
    15             p->next[x]=(Node *)malloc(sizeof(Node));
    16             p->next[x]->cut=0;
    17             for (int i=0;i<26;i++) p->next[x]->next[i]=NULL;
    18         }
    19         p=p->next[x];
    20         p->cut++;
    21     }
    22 }
    23 int Find(char *s)
    24 {
    25     Node *p=root;
    26     for (int i=0;s[i];i++)
    27     {
    28         int x=s[i]-'a';
    29         if (p->next[x]==NULL) return 0;
    30         p=p->next[x];
    31     }
    32     return p->cut;
    33 }
     1 ///字典树模板链表版
     2 struct Node
     3 {
     4     int next[26];
     5     int cut;
     6     void Init()
     7     {
     8         for (int i=0;i<26;i++) next[i]=-1;
     9         cut=0;
    10     }
    11 };
    12 Node tree[1000005];
    13 int cut=0;
    14 void inser(char *s)
    15 {
    16     int p=0;
    17     for (int i=0;s[i];i++)
    18     {
    19         int x=s[i]-'a';
    20         if (tree[p].next[x]==-1)
    21         {
    22             tree[p].next[x]=++cut;
    23             tree[cut].Init();
    24         }
    25         p=tree[p].next[x];
    26         tree[p].cut++;
    27     }
    28 }
    29 int Find(char *s)
    30 {
    31     int p=0;
    32     for (int i=0;s[i];i++)
    33     {
    34         int x=s[i]-'a';
    35         if (tree[p].next[x]==-1) return 0;
    36         p=tree[p].next[x];
    37     }
    38     return tree[p].cut;
    39 }
  • 相关阅读:
    Java 多态
    HDFS读写原理
    HDFS详解
    Servlet基础
    Tomcat
    HTTP简介
    JDBC技术
    final、finally和finalize
    java 中的权限修饰符
    进程、线程、线程状态、多线程实现方法
  • 原文地址:https://www.cnblogs.com/pblr/p/5720260.html
Copyright © 2011-2022 走看看