zoukankan      html  css  js  c++  java
  • 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;
    }
    



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

  • 相关阅读:
    php 二维数组索引乱序 shuffle() 函数;
    php-m 与 phpinfo 不一致的解决办法
    javascript 数组去重
    javascript 闭包实现的5种方法
    javascript 下 function 和 Function的区别
    解决ThinkPhp在nginx下404问题
    TP5.1中的验证类 validate用法
    webstrom 快捷键
    css中可以和不可以继承的属性
    封装 class 类 js
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785913.html
Copyright © 2011-2022 走看看