hdu 1671 Phone List

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.