Trie树

Trie树关于前缀计算的应用:

HDUOJ   1251统计难题   http://acm.hdu.edu.cn/showproblem.php?pid=1251

HDUOJ   1671PhoneList   http://acm.hdu.edu.cn/showproblem.php?pid=1671


统计难题 

题意就是给出几个word  再给几个word问有几个是已这几个word作为前缀的word

直接模版就可以了

PhoneList

题意就是问给的一系列数字组合中有木有数字组合是其他数字组合的前缀  

要把所有输入的数字组合都保存insert  然后find  看find的值是否大于1

敲了板子之后提交报错  内存超了   看了别人的代码  发现就是要释放内存  加个release函数 

贴个代码:

#include <iostream>
#include <cstdio>
#include <cstring>


using namespace std;


char a[10005][10];
struct node
{
    int cnt;
    node *next[10];
    node()
    {
        for(int i=0;i<10;i++)  next[i]=NULL;
    }
};


node *root;
void insert(char *str)
{
    node *p,*newnode;
    p=root;
    for(;*str;str++)
    {
        if(p->next[*str-'0']!=NULL)
        {
            p=p->next[*str-'0'];
            p->cnt++;
        }
        else
        {
            newnode=new node;
            p->next[*str-'0']=newnode;
            p=p->next[*str-'0'];
            p->cnt=1;
        }
    }
}


int find(char *str)
{
    node *p;
    p=root;
    for(;*str;str++)
    {
        if(p->next[*str-'0']!=NULL)  p=p->next[*str-'0'];
        else return 0;
    }
    return p->cnt;
}


void release(struct node *now)//释放内存
{
    for(int i=0;i<10;i++)
        if(now->next[i])
            release(now->next[i]);
    delete now;
}


int main()
{
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        root=new node;
        root->cnt=0;
        int n;
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            insert(a[i]);
        }
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(find(a[i])>1)
            {
                flag=1;
                break;
            }
            else
                continue;
        }
       // cout<<flag<<endl;
        if(flag)
            puts("NO");
        else
            puts("YES");
        release(root);
    }
    return 0;
}



语言翻译应用

HD 1075   

题意就是将火星文翻译成英文输出

http://acm.hdu.edu.cn/showproblem.php?pid=1075


insert()将模版中的cnt换成翻译后的字符串  绑在要被翻译的字符串最后一个字符上

find()依次判断每个字符是不是跟初始状态一样  加上对最后一个字符的判断

一开始用 char *s;  简直蛋疼  指针真的好难搞

string 有时还是蛮好用的  有些函数还是值得用的

贴个代码:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>


using namespace std;


struct node
{
    string s;
    node *next[26];
    node()
    {
        s="";
        memset(next,0,sizeof(next));
    }
};


node *root;


void insert(string s1,string s2)
{
    node *p;
    p=root;
    for(int i=0;i<s2.size();i++)
    {
        if(p->next[s2[i]-'a']==NULL)
            p->next[s2[i]-'a']=new node();
        p=p->next[s2[i]-'a'];
    }
    p->s=s1;
}


void find(string s)
{
    //cout<<" "<<s<<" ";
    node *p;
    p=root;
    for(int i=0;i<s.size();i++)
    {
        int num=s[i]-'a';
        if(p->next[num]==NULL)
        {
            cout<<s;
            return;
        }
        p=p->next[num];
    }
    if(p->s=="")  cout<<s;
    else  cout<<p->s;
}


int main()
{
    string s,s1,s2;
    root=new node();
    cin>>s;//start
    while(cin>>s1&&s1!="END")
    {
        cin>>s2;
        //cout<<s1<<"  "<<s2<<endl;
        insert(s1,s2);
    }
    cin>>s;//start
    getchar();//\n
    while(getline(cin,s)&&s!="END")
    {
        int n=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]<'a'||s[i]>'z')
            {
                s2=s.substr(n,i-n);//截取字符串  j~i-1
               // cout<<endl<<s1<<endl;
                find(s2);
                cout<<s[i];
                n=i+1;
            }
        }
        cout<<endl;
    }
    return 0;
}


//待续  欢迎大家评论 给点建议


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值