hdu 1251 統計

來源:互聯網
上載者:User

標籤:style   io   for   html   re   c   

他妹的,敲完了,電腦死機了,全部消失了,又從新打了一遍,。。。這是什麼節奏
#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\n", i);    }        return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.