字典树学习

字典树学习_第1张图片


题目描述:假设有很多个单词输入。输出最后出现次数最多的那个。


#include 
#include 
#include 
#include 
using namespace std ;

struct DicTree
{
    struct DicTree * next[26] ;
    int cnt ;
}*a;

void init()
{
    a = new DicTree ;
    for(int i = 0 ;i<26 ;i++)
    {
        a->next[i] = NULL ;
    }
}

int insert(char str[])
{
    int len = strlen(str) ;
    DicTree * head = a ;
    for(int i = 0 ;inext[res] == NULL)
        {
            head->next[res] = new DicTree ;
            head = head->next[res] ;
            head->cnt = 0 ;
            for(int j = 0 ;j<26 ;j++)
            {
                head->next[i] = NULL ;
            }
        }
        else
        {
            head = head->next[res] ;
        }
    }
    head->cnt++ ;
    return head->cnt ;
}

int main()
{
    int num ;
    int maxNum ;
    int tmp ;
    char str[100],ans[100] ;
    scanf("%d",&num) ;
    init() ;
    maxNum = 0 ;
    for(int i = 0 ;i maxNum)
        {
            maxNum = tmp ;
            strcpy(ans,str) ;
        }
    }
    printf("the max num is %d   the word is %s\n",maxNum,ans) ;
    return 0 ;
}


你可能感兴趣的:(算法学习笔记)