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;
    }

      

  • 相关阅读:
    c++字符串排序
    JAVA实现四则运算的简单计算器
    JAVA图形小动画之简单行星运动
    JAVA多线程编程
    ege图形库之简单贪吃蛇(c++)
    ege图形库之动画排序
    mysql 性能优化方案
    MYSQL 优化常用方法
    [手把手教你] 用Swoft 搭建微服务(TCP RPC)
    php有效防止同一用户多次登录
  • 原文地址:https://www.cnblogs.com/vongang/p/2122983.html
Copyright © 2011-2022 走看看