標籤:io for re c html ar
#include <stdio.h>#include <string.h>#include <stdlib.h>#define ZERO 0const int FIRST_CHAR = '0';char num[11111][20] ;typedef struct node{ struct node *child[20]; /* 儲存下一個字元 */ 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; /*還沒比較完就出現不匹配, 字典樹中沒有此單詞*/ } } if((current->n)>1) return 1; return 0; }void release(Node root){ int i; if (NULL == root) { return; } for (i = 0; i < 20; i++) { if ( root->child[i] != NULL ) { release( root->child[i] ); } } free( root ); root = NULL;}int main(){ int i,n,m,check; scanf("%d",&n); while(n--) { scanf("%d",&m); root = (Node)calloc(1, sizeof(node));//錯了N次 for(i=0;i<m;i++) { scanf("%s",num[i]); insert(num[i]); } check=0; for(i=0;i<m;i++) { if(find_word(num[i])) { check=1; break; } } if(check==1) printf("NO\n"); if(check==0) printf("YES\n"); release(root); } return 0;}