標籤: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;}