Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

错在那里呢?大牛看看你就知道了?帮帮我!

Posted by chenxuan123456789 at 2012-08-05 15:38:42 on Problem 2001
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define M 26
#define N 1000+10
char str1[N][30];
typedef struct Trie1
{
	struct Trie1 *next[M];
	int val;
}Trie;
Trie *root;
void init()
{
	int i;
	root=(Trie*)malloc(sizeof(Trie));
	for(i=0;i<M;i++)
	{
		root->next[i]=NULL;
		root->val=0;
	}
}
void bulit(Trie *T,char str2[])
{
	int i,id,length,j;
	Trie *p,*q;
	p=root;
	length=strlen(str2);
	for(i=0;i<length;i++)
	{
		id=str2[i]-'a';
		if(p->next[id]==NULL)
		{
			q=(Trie*)malloc(sizeof(Trie));
			for(j=0;j<M;j++)
			{
				q->next[j]=NULL;
				q->val=0;
			}
			p->next[id]=q;
			p->val++;
			p=q;
		}
		else
		{	
			p->val++;
			p=p->next[id];
		}
	}
}
void find(Trie *T,char str[])
{
	int i,id,length;
	Trie *p=T;
	length=strlen(str);
	printf("%s ",str);
	for(i=0;i<length;i++)
	{
		id=str[i]-'a';
		p=p->next[id];
	    printf("%c",str[i]);
		if(p->val==1)
	    break;
	}
}
int main()
{
	int i,n=0;
	init();
	while(scanf("%s",str1[n])!=EOF)
	{
		bulit(root,str1[n]);
		n++;
	}
	for(i=0;i<n;i++)
	{
		find(root,str1[i]);
		printf("\n");
	}
    return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator