HihoCoder第二周与POJ3630:Trie树的建立

这又是两道一样的题,都是建立trie树的过程。

HihoCoder第二周:

这里其实逻辑都很简单,主要在于数据结构struct的使用。

#include <iostream>
#include <cstring>
using namespace std;
 
struct node{
    int get;
    node *branch[26];
};

node* create()
{
    node* tnew = (node *)malloc(sizeof(node));
    
    tnew->get = 0;
    
    for(int i = 0;i < 26 ;i++)
    {
        tnew->branch[i] = NULL;
    }
    return tnew;
}

int main()
{
    long n,m;
    char str[35];
    int len,i;

    cin>>n;
    
    node* root = create();
    
    node* temp = NULL;

    while(n--)
    {
       cin>>str;
       
       len = strlen(str);

       temp = root;
       
       for(i=0;i< len;i++)
       {
           if(NULL==temp->branch[str[i]-'a'])
           {
               temp->branch[str[i]-'a']=create();
           }
           temp->branch[str[i]-'a']->get++;
           temp = temp->branch[str[i]-'a'];
       }
    
    }

    cin>>m;
    while(m--)
    {
        cin>>str;
        
        len = strlen(str);
        
        temp = root;

        for(i=0;i<len;i++)
        {
           temp = temp->branch[str[i]-'a'];
           if(temp==NULL)
           {
               break;
           }
        }
        if(temp==NULL)
            cout<<0<<endl;
        else
            cout<<temp->get<<endl;
    }

    return 0;
}

POJ3630:

POJ这里的话,有一点变化,就是不能用动态建立树,会超时。另外一点初始化,我之前一直没把child初始化为NULL,导致被坑了很多次。

#include <iostream>
#include <cstring>
using namespace std;

struct node{
    int get;
    node *child[10];
}Node[100005];

void start()
{
    int i;
    node* temp;
    for(i=0;i<100005;i++)
    {
        temp=&Node[i];
        temp->get=0;
        for(int j =0;j<10;j++)
            temp->child[j]=NULL;
    }
}
int nodenum=1;

int test(char str[])
{
    int len = strlen(str);

    node* root = &Node[0];
    node* temp = root;

    for(int i=0; i<len; i++)
    {
        
        if(i==len-1)
        {
            if (temp->child[str[i]-'0']!=NULL)
            {
                return 0;
            }
            else
            {
                temp->child[str[i]-'0']= &Node[nodenum++];

                temp->child[str[i]-'0']->get++;
            }
        }
        else
        {
            if(temp->child[str[i]-'0']==NULL)
            {
                temp->child[str[i]-'0']=&Node[nodenum++];
            }
            else
            {
                if(temp->child[str[i]-'0']->get!=0)
                {
                    return 0;
                }
            }
            temp=temp->child[str[i]-'0'];
        }
    }
    return 1;
}

int main()
{
    int m,n;
    int flag=1,flag2=1;
    char str[35];	

    cin>>m;

    while(m--)
    {
        cin>>n;
        start();
        nodenum=1;
        flag=1;flag2=1;
        while(n--)
        {
            cin>>str;
            flag =test(str);

            if(flag==0)
                flag2=0;
        }
        if(flag2)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    
    return 0;
}

    原文作者:Trie树
    原文地址: https://blog.csdn.net/u010885899/article/details/45367041
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞