zoukankan      html  css  js  c++  java
  • hdu 1251 统计

    他妹的。敲完了。电脑死机了,所有消失了,又从新打了一遍,。。。

    这是什么节奏

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define ZERO 0
    #define ALPH_LEN 26 /* 26个字母 */
    const char FIRST_CHAR = 'a';
    
    typedef struct node
    {
        struct node *child[ALPH_LEN]; /* 存储下一个字符 */
        int n; /* 记录当前单词出现的次数 */
    }node, *Node;
    
    Node root; /* 字典树的根结点(不存储不论什么字符) */
    /* 插入单词 */
    void insert(char *str)
    {
        int i, index, len;
        Node current = NULL, newnode = NULL;
        
        len = strlen(str);
        
        current = root; /* 開始时当前的结点为根结点 */
        for (i = 0; i < len; i++) /* 逐个字符插入 */
        {
            index = str[i] - FIRST_CHAR; /* 获取此字符的下标 */
            if (current->child[index] != NULL) /* 字符已在字典树中 */
            {
                current = current->child[index]; /* 改动当前的结点位置 */
                (current->n)++; /* 当前单词又出现一次, 累加 */
            }
            else /* 此字符还没出现过, 则新增结点 */
            {
                newnode = (Node)calloc(1, sizeof(node)); /* 新增一结点, 并初始化 */
                current->child[index] = newnode;
                current = newnode; /* 改动当前的结点的位置 */
                current->n = 1; /* 此新单词出现一次 */
            }
        }
    }
    /* 在字典树中查找单词 */
    int find_word(char *str)
    {
        int i, index, len;
        Node current = NULL;
        
        len = strlen(str);
        
        current = root; /* 查找从根结点開始 */
        for (i = 0; i < len; i++)
        {
            index = str[i] - FIRST_CHAR; /* 获取此字符的下标 */
            if (current->child[index] != NULL) /* 当前字符存在字典树中 */
            {
                current = current->child[index]; /* 改动当前结点的位置 */
            }
            else
            {
                return ZERO; /*还没比較完就出现不匹配, 字典树中没有此单词*/
            }
        }
        
        return current->n; /* 此单词出现的次数 */
    }
    
    int main()
    {
        char tmp[11];
        int i;
        
        root = (Node)calloc(1, sizeof(node));
    
        while (gets(tmp), strcmp(tmp, "") != ZERO)
        {
            insert( tmp );
        }
        
        while (scanf("%s", tmp) != EOF)
        {
            i = find_word( tmp );
            printf("%d
    ", i);
        }
        
    
    
        return 0;
    }
    

  • 相关阅读:
    108.将有序数组转换成二叉搜索树
    剑指Offer 09.用两个栈实现队列
    215.数组中的第K个最大元素
    AcWing 845.八数码
    1.两数之和
    迷宫问题
    倒计时
    Redis 红包
    Redis
    EF Api 全套
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6822335.html
Copyright © 2011-2022 走看看