zoukankan      html  css  js  c++  java
  • HDU 1671 Phone List(字典树)

      字典树,注意释放内存,否则MLE

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    char a[10010][20];
    struct node
    {
        int num;
        node *childs[10];
        node()
        {
            num = 0;
            for(int i = 0; i < 10; i++)
                childs[i] = NULL;
        }
    };
    node *root = new node;
    node *nownode,*newnode;
    void Insert(char *str)
    {
        nownode = root;
        int lens = strlen(str);
        for(int i = 0; i < lens; i++)
        {
            int m = str[i] - '0';
            if(nownode->childs[m] != NULL)
            {
                nownode = nownode->childs[m];
                nownode->num++;
            }
            else
            {
                newnode = new node;
                ++newnode->num;
                nownode->childs[m] = newnode;
                nownode = nownode->childs[m];
            }
        }
    }
    int Find(char *str)
    {
        nownode = root;
        int lens = strlen(str);
        for(int i = 0; i < lens; i++)
        {
            int m = str[i] - '0';
            nownode = nownode->childs[m];
        }
        return nownode->num;
    }
    void del(node *root)
    {
        for(int i=0; i<10; i++)
        {
            if(root->childs[i]!=NULL)
            {
                del(root->childs[i]);
            }
        }
        delete(root);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            root = new node;
            scanf("%d",&n);
            for(int i = 0; i < n; i++)
            {
                scanf("%s",a[i]);
                Insert(a[i]);
            }
            bool flag = true;
            for(int i = 0; i < n; i++)
            {
                if(Find(a[i]) != 1)
                {
                    flag = false;
                    break;
                }
            }
            if(flag)puts("YES");
            else puts("NO");
            del(root);
        }
        return 0;
    }
  • 相关阅读:
    fabrci网络调优
    fabric链码容器
    fabric文档查阅
    fabric基础设施管理-(五)移除网络
    fabric源码编译
    fabric网络状态监控
    fabric基础设施管理-(六)配置管理
    Scheme宏基础入门(转载)
    GO语言程序中解决中文日期格式的解析问题
    临别之际
  • 原文地址:https://www.cnblogs.com/jifahu/p/5449417.html
Copyright © 2011-2022 走看看