zoukankan      html  css  js  c++  java
  • POJ——3630 Phone List

           这道题有很多种解法,可以用qsort,也可以用Trie树,Trie树做时需要开静态数组开辟空间,动态分配会TLE!在Discuss里看到两组数据,注意一下

    2
    2
    1
    12
    2
    12
    1
    NO
    NO

    下面是Trie树做法,在做题之前被某人提醒静态数组不要开得太大,否则会MLE,结果我就悲剧的RE了一晚上!!

     2780K    110MS

    View Code
    #include <stdio.h>
    #include
    <string.h>
    typedef
    struct node
    {
    int flag;
    struct node * next[10];
    }node;
    node num[
    500000];
    int x = 0;
    int flg;
    node
    * newnode()
    {
    node
    * p = num + x++;
    int i;
    for(i = 0; i < 10; i++)
    {
    p
    ->next[i] = NULL;
    }
    p
    ->flag = 0;
    return p;
    }
    void insert(node * root, char *s)
    {
    int len = strlen(s);
    int i, k;
    node
    * p = root;
    for(i = 0; i < len; i++)
    {
    k
    = s[i] - '0';
    if(i == len - 1 && p->next[k] != NULL)
    {
    flg
    = 1;
    return;
    }
    if(p->next[k] != NULL)
    {
    if(p->next[k]->flag)
    {
    flg
    = 1;
    return;
    }
    p
    = p->next[k];
    }
    else
    {
    p
    ->next[k] = newnode();
    p
    = p->next[k];
    }
    }
    p
    ->flag = 1;
    }
    int main()
    {
    int t, n;
    char s[20];
    scanf(
    "%d", &t);
    while(t--)
    {
    x
    = 0; flg = 0;
    node
    * root = newnode();
    scanf(
    "%d", &n);
    while(n--)
    {
    scanf(
    "%s", s);
    if(!flg) insert(root, s);
    }
    if(flg) printf("NO\n");
    else printf("YES\n");
    }
    return 0;
    }

      

  • 相关阅读:
    [bzoj4868][Shoi2017]期末考试
    [4.30四校联考]
    [Noi2013]快餐店
    [Noi2013]树的计数
    [Noi2013]向量内积
    [Noi2013]矩阵游戏
    java端拦截器判断客户的的请求是否是ajax请求
    spring 事务回滚
    Page directive: illegal to have multiple occurrences of contentType with different values
    jsp service bean
  • 原文地址:https://www.cnblogs.com/vongang/p/2122983.html
Copyright © 2011-2022 走看看