zoukankan      html  css  js  c++  java
  • 字典树:求以某字符串开始的单词个数

    http://hihocoder.com/contest/hiho2/problem/1

    与之前做的有点像:http://blog.csdn.net/u011644423/article/details/37833905

    稍微改下,注意每次在建立Trie树时同时进行统计

      1 #include <stdio.h>  
      2 #include<stdlib.h>  
      3 #define MAX 26  
      4 //using namespace std;   
      5 typedef struct TrieNode                     //Trie结点声明   
      6 {  
      7     //bool isStr;      
      8     int count;  
      9     //标记该结点处是否构成单词 可能大于一 
     10     struct TrieNode *next[MAX];            //儿子分支   
     11 }Trie;  
     12    
     13 void insert(Trie *root,const char *s)     //将单词s插入到字典树中   
     14 {  
     15     if(root==NULL||*s=='')  
     16         return;  
     17     int i;  
     18     Trie *p=root;  
     19     while(*s!='')  
     20     {  
     21         if(p->next[*s-'a']==NULL)        //如果不存在,则建立结点   
     22         {  
     23             Trie *temp=(Trie *)malloc(sizeof(Trie));  
     24             for(i=0;i<MAX;i++)  
     25             {  
     26                 temp->next[i]=NULL;
     27                   
     28             }  
     29            // temp->isStr=false;  
     30             p->next[*s-'a']=temp;  
     31             p=p->next[*s-'a'];    
     32             p->count=1;
     33         }     
     34         else  
     35         {  
     36             p=p->next[*s-'a'];  
     37             (p->count)++;  
     38         }  
     39         s++;  
     40     }  
     41    // p->isStr=true;                       //单词结束的地方标记此处可以构成一个单词   
     42 }  
     43 
     44 int findmin(Trie *root,char *str){  
     45 Trie *p=root;  
     46 int num=0;
     47 while(*str!=''){  
     48     if(p->next[*str-'a']==NULL){  
     49        num=0; break;  
     50     }  
     51 
     52     num=(p->next[*str-'a'])->count;
     53 //    printf("%c",*str);  
     54     p=p->next[*str-'a'];  
     55     str++;  
     56   
     57 }  
     58   return num;
     59 }  
     60 void del(Trie *root)                      //释放整个字典树占的堆区空间   
     61 {                                           //递归  
     62     int i;  
     63     for(i=0;i<MAX;i++)  
     64     {  
     65         if(root->next[i]!=NULL)  
     66         {  
     67             del(root->next[i]);  
     68         }  
     69     }  
     70     free(root);  
     71 }  
     72    
     73 int main(int argc, char *argv[])  
     74 {  
     75     int i,j;  
     76   //  int n,m;                              //n为建立Trie树输入的单词数,m为要查找的单词数   
     77      
     78     Trie *root= (Trie *)malloc(sizeof(Trie));  
     79     for(i=0;i<MAX;i++)  
     80     {  
     81         root->next[i]=NULL;  
     82     }  
     83     //root->isStr=false;  
     84     //scanf("%d",&n);  
     85     //getchar();  
     86    
     87     int n;
     88     scanf("%d",&n);
     89     for(j=1;j<=n;j++){
     90         getchar();  
     91         char s[12];
     92         scanf("%s",s);
     93         insert(root,s);     
     94     }
     95 //    while(scanf("%s",s[gsum])!=EOF)  
     96 //        //scanf("%s",s);  
     97 //    {  
     98 //          
     99 //        insert(root,s[gsum]);  
    100 //        getchar();  
    101 //        gsum++;  
    102 //    }  
    103      int m;
    104     scanf("%d",&m);
    105     
    106     for(j=1;j<=m;j++){
    107         getchar();  
    108         char s2[12];
    109         scanf("%s",s2);
    110         printf("%d
    ",findmin(root,s2));   
    111     }
    112      
    113         
    114       
    115    // del(root);                         //释放空间很重要   
    116     return 0;  
    117 }  

    随便说下。 用博客园写博客比csdn爽太多

  • 相关阅读:
    BA 的职责
    How to grow up as a BA
    打开struts-config.xml 报错 解决方法Could not open the editor
    eclipse中svn插件的工程不能与svn资源库同步的解决方法
    三种常用的MySQL建表语句(转)
    MySQL中CURRENT_TIMESTAMP(转)
    JavaWeb之CSS详解
    如何在Mac OSX系统下安装Tomcat
    JavaWeb之XML详解
    JavaWeb之HTML入门及常用标签
  • 原文地址:https://www.cnblogs.com/france/p/4808921.html
Copyright © 2011-2022 走看看