zoukankan      html  css  js  c++  java
  • 动态字典树_字串查找匹配(HDU_1075)

    #include <stdio.h>
    #include <string.h>
    
    #define M 3002
    #define MU 26
    #define WLEN 12
    
    struct node{
        char to[WLEN];
        node *child[MU];
        node()
        {
            to[0] = '';
            memset(child,NULL,sizeof(child));
        }
    };
    
    char getTr[M],findEd[WLEN];
    
    void clear(node *root)
    {
        for(int i=0;i<MU;i++)
        {
            if(root->child[i] != NULL)
                clear(root->child[i]);
        }
        delete root;
    }
    
    void insert(node *root, char *str, char *to)
    {
        node *p = root;
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            int k = str[i] - 'a';
            if(p->child[k] == NULL)
            {
                p->child[k] = new node;
            }
            p = p->child[k];
        }
        strcpy(p->to,to);
    }
    
    void find(node *root, char *str)
    {
        node *p = root;
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            int k = str[i] - 'a';
            if(p->child[k] == NULL)
                return ;
            p = p->child[k];
        }
        strcpy(findEd,p->to);
    }
    
    bool isChar(char ch)
    {
        if(ch >= 'a' && ch <= 'z')    return true;
        return false;
    }
    
    void run()
    {
        node *root = new node;
        char s1[WLEN],s2[WLEN];
        scanf("%s",s1);
        while(scanf("%s",s1))
        {
            if(!strcmp(s1,"END"))    break;
            scanf("%s",s2);
            insert(root,s2,s1);
        }
        
        scanf("%s",s1);
        getchar();
        while(gets(getTr))
        {
            if(!strcmp(getTr,"END"))    break;
            int j = 0;
            int len = strlen(getTr);
            for(int i=0; i<len; i++)
            {
                if(isChar(getTr[i]))
                {
                    s2[j++] = getTr[i];
                }
                else
                {
                    s2[j] = '';
                    findEd[0] = '';
                    find(root,s2);
                    if(findEd[0] != '')    printf("%s",findEd);
                    else                printf("%s",s2);
                    printf("%c",getTr[i]);
                    j = 0;
                }
            }
            printf("
    ");
        }
        clear(root);
    }
    
    int main(int argc, char *argv[])
    {
    #ifdef __MYLOCAL
        freopen("in.txt","r",stdin);
    #endif
        
        run();
        
        return 0;
    }
  • 相关阅读:
    【BZOJ 4151 The Cave】
    【POJ 3080 Blue Jeans】
    【ZBH选讲·树变环】
    【ZBH选讲·拍照】
    【ZBH选讲·模数和】
    【CF Edu 28 C. Four Segments】
    【CF Edu 28 A. Curriculum Vitae】
    【CF Edu 28 B. Math Show】
    【CF Round 439 E. The Untended Antiquity】
    【CF Round 439 C. The Intriguing Obsession】
  • 原文地址:https://www.cnblogs.com/lk1993/p/3219513.html
Copyright © 2011-2022 走看看