[字典树 ] poj 3630 Phone list

这篇博客介绍如何利用字典树(Trie Tree)解决POJ 3630题目,即判断一组字符串中是否存在某个字符串是其他字符串的前缀。文章通过C++实现了一个字典树结构,并展示了如何插入字符串以及检查前缀。在处理过程中,博主发现不能动态开辟空间会导致TLE(Time Limit Exceeded)错误。
摘要由CSDN通过智能技术生成
/**
字典树
判断是否存在一个字符串为另一个的前缀。

字典树果然还是不能动态开辟空间呢,TLE的伤不起鸟。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
#define N 100000
struct tireTree
{
    bool isNum;
    tireTree *tr[10];
}tt[N];

struct Phone
{
    char str[11];
    int len;
}s[N];

bool cmp(Phone a,Phone b)
{
    return a.len < b.len;
}
tireTree *ps;
bool insert(string s,tireTree *&rt)
{
    int num;
    if(rt == NULL)
        rt = ps++;
    tireTree *loc = rt;
    for(int i = 0; s[i]; ++i)
    {
        num = s[i] - '0';
        if(loc->tr[num] == NULL)
            loc -> tr[num] = ps++;
        loc = loc-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值