POJ - 3630 代码

Trie的简单应用,只涉及插入字符串的操作。

需要注意的是,输入数据有T组,在处理每一组数据之前都要初始化root,由于忽视了这一点WA了n次。

还有一点就是,在发现一组数据答案为“NO”之后,仍然要读完这组数据的字符串。在这一点上也WA了好多次= =

另外,本题大概需要建立4000000个节点,如果采用动态空间建点会TLE。

看来malloc操作确实是很慢啊。

所以提前开一个大的数组空间,静态的就好了。

代码如下:

/*
Author : Magician Van
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>

using namespace std;

const int MaxNext = 10 + 2, MaxL = 10 + 2, MaxNode = 100000 + 5;

int T, n, top;

char Str[MaxL];

bool Wrong = false;

typedef struct TrieNode
{
    bool isStr;
    TrieNode *Next[MaxNext];
} Trie;

Trie Ta[MaxNode];

void Insert(Trie *root, char *S, int len)
{
    if (root == NULL || len == 0) return;
    Trie *p = root;
    int sum = 0;
    for (int i = 0; i <= len - 1; i++)
    {
        if (p -> Next[S[i] - ‘0‘] == NULL)
        {
            Trie *temp = &Ta[++top];
            temp -> isStr = false;
            for (int j = 0; j <= 9; j++) temp -> Next[j] = NULL;
            p -> Next[S[i] - ‘0‘] = temp;
        }
        else sum++;
        p = p -> Next[S[i] - ‘0‘];
        if (p -> isStr)
        {
            Wrong = true;
            return;
        }
    }
    p -> isStr = true;
    if (sum == len) Wrong = true;
} 

int main()
{
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        top = 0;
        Trie *root = &Ta[++top];
        for (int i = 0; i <= 9; i++) root -> Next[i] = NULL;
        root -> isStr = false;
        Wrong = false;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%s", Str);
            if (!Wrong) Insert(root, Str, strlen(Str));
        }
        if (Wrong) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

POJ - 3630 代码

时间: 2024-08-10 15:10:06

POJ - 3630 代码的相关文章

poj 3630 Phone List (字典树 +静态字典树)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22235   Accepted: 6885 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

POJ - 2418 代码

使用Trie树完成.比STL map 快很多. 输出时DFS,用一个字符数组记录当前字符串. 走到是字符串的结点就输出. 代码如下. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int MaxL =

POJ 3630 Trie

链接: http://poj.org/problem?id=3630 题意: 给你n个字符串,判断有没有字符串是其他字符串的前缀 题解: 建一个字典树,在插入的过程中,如果没有新建一个结点,那这个字符串肯定是其他字符串的前缀, 如果新建结点的时候发现,有的字符串以这个字符结尾,那肯定有字符串是这个字符串的前缀 代码: 31 int pi = 1; 32 int fg; 33 34 struct Node { 35 int next[10]; 36 bool end; 37 }tree[MAXN]

poj 3630 Phone List(字典树)

题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先前的字符串为其前缀: (1)若该字符串为某个字符串前缀,则存在一条从根节点到该字符串的最后一个字符串的路径; (2)若存在某个字符串为该字符串前缀,则在该字符串的查找路径中存在一条子路径,路径的最后的结点的endOfWord 标记为true,表示存在某个字符串为其前缀: 代码: #include <

POJ 3630 Phone List(字典树)

题目链接:http://poj.org/problem?id=3630 题意:给定n个字符串.判断是否存在其中某个字符串为另外一个字符串的前缀.若不存在输出YES.否则输出NO. 思路:裸的字典树. 代码 #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string>

关于部分应用无法向POJ提交代码的解决方案

有个一年没做过题了,最近有骚年反映他们的VirtualJudge无法做POJ的题目,一直都是JudgeError状态. 于是登录到那个VJudge试了试,代码的确一直无法提交成功,他们的服务器发回500错误代码. 试了试自己写的voj内核,果然有问题,又试了一下国内其他的Vjudge都没法交到POJ,但是是用HUST最新的VJudge提交POJ没问题,这个问题他们应该已经早处理好了. 那么问题应该在代码post的部分,可能是pojpost代码的协议有一些变化.于是登陆到poj的提交代码界面,先试

POJ 3630 Phone List(trie树的简单应用)

题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插入的过程中维护到当前节点是不是字符串这个布尔量即可,同时判断是否存在上述情况. code: 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5

POJ 1035 代码+详细注释

Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19319 Accepted: 7060 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words us

POJ 3630 Phone List

Phone List Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 363064-bit integer IO format: %lld      Java class name: Main Given a list of phone numbers, determine if it is consistent in the sense that no numbe