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



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【安徽集训】fiend
    【安徽集训】Emerald
    【安徽集训】Entropy
    【安徽集训】字符串
    【福建集训】果树
    【CF335 E】Counting Skyscrapers
    【BZOJ 3514】Codechef MARCH14 GERALD07 加强版
    【未知来源】Randomized Binary Search Tree
    【ZJOI 2016】旅行者
    【AGC002 E】Candy Piles
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785913.html
Copyright © 2020-2023  润新知