zoukankan      html  css  js  c++  java
  • hdoj 1671字典树水题之三 静态数组节约内存法

    Phone List

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6265    Accepted Submission(s): 2131


    Problem Description
    Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
    1. Emergency 911
    2. Alice 97 625 999
    3. Bob 91 12 54 26
    In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
     
    Input
    The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
     
    Output
    For each test case, output “YES” if the list is consistent, or “NO” otherwise.
     
    Sample Input
    2
    3
    911
    97625999
    91125426
    5
    113
    12340
    123440
    12345
    98346
     
    Sample Output
    NO
    YES
     
    题目思路:主要判断是否有相互的前缀,加一个标记即可。
    注意:如果单纯的用malloc去申请内存,内存会超出,这里用一个数组去存,每个集体,从0开始,这样就节约了内存。如果用free或者move会浪费时间!
    代码:
    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>
    typedef struct node
    {
    	int end;
    	struct node *next[10];
    }node;
    int k;
    node m[1000001];//
    
    int insert(char *str,node *T)
    {
    	node *p,*q;
    	int len,flag=0,id,i,j;
    	p=T;
    	len=strlen(str);
    	for(i=0;i<len;++i)
    	{
    		id=str[i]-'0';
    		if(p->end==1)
    			return 1;//判断是否存在较短的串是他的前缀
    		if(p->next[id]==NULL)
    		{
    		//	q=(node*)malloc(sizeof(node));
    		//	node *q=new node();
    			q=&m[k++];
    			for(j=0;j<10;j++)
    			{
    				q->end=0;
    				q->next[j]=NULL;
    			}
    			p->next[id]=q;
    		}
    	/*	if(i==len-1)
    			p->end=1;*/
    		p=p->next[id];
    	}
    	for(i=0;i<10;++i)
    		if(p->next[i]!=NULL)
    			return 1;//判断是否有比他长的串
    	p->end=1;
    	return flag;
    }
    
    
    int main()
    {
    	int t,n,flag,i;
    	char str[11];
    	node *T;
    	scanf("%d",&t);
    	while(t--)
    	{
    		//node *T=new node();
    		k=0;
    		T=&m[k++];
    		//T=(node*)malloc(sizeof(node));
    		for(i=0;i<10;++i)
    		{
    			T->end=0;
    			T->next[i]=NULL;
    		}
    		scanf("%d",&n);
    		flag=0;
    		while(n--)
    		{
    			scanf("%s",&str);
    			if(insert(str,T))
    				flag=1;
    		}
    		if(flag)
    			printf("NO\n");
    		else
    			printf("YES\n");
    		//free(T);
    	}
    	return 0;
    }
    
  • 相关阅读:
    vim删除某一列
    linux下在当前文件夹查找一个字符串信息
    .tar和.tar.gz的区别
    visual studio中调用masm汇编
    iconv转码失败的原因
    终端查看
    kubectl 常用命令总结
    Charles实战之Charles抓取https请求
    Fiddler实战之拟2G、3G、4G网络进行弱网测试
    Fiddler实战之使用Fiddler模拟弱网环境
  • 原文地址:https://www.cnblogs.com/zibuyu/p/3022422.html
Copyright © 2011-2022 走看看