• HDU 1251 裸的字典树、入门题


    裸的字典树还是挺简单的、

    四个基本操作建立、查找、插入、删除

    建立新结点我是用的c++中 new操作、当然也可以用malloc,都方便

    不过指针阿、地址阿、这其中关系什么的我貌似还不是很清楚阿、

    因为刚开始我的头结点也是定义的指针、然后程序就炸了、我不清楚原因呢、

    有待弄清楚、

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstring>
     6 typedef struct node
     7 {
     8     int cnt;
     9     struct node *next[26];
    10     node()
    11     {
    12         cnt=0;
    13         memset(next,0,sizeof(next));
    14     }
    15 }t;
    16 node root;        //头结点 
    17 void save(char *s)
    18 {
    19     int len=strlen(s);
    20     if(len==0)    return;
    21     node *p=&root;
    22     node *tmp=NULL;
    23     for(int i=0;i<len;++i){
    24         if(p->next[s[i]-'a']==NULL){
    25             tmp=new struct node;
    26             p->next[s[i]-'a']=tmp;
    27         }
    28         p=p->next[s[i]-'a'];
    29         p->cnt++;
    30     }
    31 }
    32 void findtree(char *s)
    33 {
    34     struct node *p=&root;
    35     int i,l=strlen(s);
    36     for(int i=0;i<l;++i){
    37         if(p->next[s[i]-'a']==NULL){
    38             printf("0
    ");
    39             return;
    40         }
    41         p=p->next[s[i]-'a'];
    42     }
    43     printf("%d
    ",p->cnt);
    44     return;
    45 }
    46 int main()
    47 {
    48     char s[15];
    49     while(gets(s)&&s[0]!=0)    save(s);
    50     while(~scanf("%s",s))
    51         findtree(s);
    52     return 0;
    53 }
  • 相关阅读:
    android 视频监控相关(打包下载)
    Git command
    logstash同步千万级数据问题
    mac 安装redis
    本地vue测试post请求需要跨域,axios总会发送一个options请求解决方法
    git commit规范
    关于本博客样式
    yarn全局安装并解决在vscode中报错
    Everything的使用技巧
    开始我的asp.net之旅
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5354471.html
Copyright © 2020-2023  润新知