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

大牛帮帮忙, sample都能过,提交总是RE,

Posted by hokey at 2008-02-02 21:49:31 on Problem 2001
#include <stdio.h>
#include <string.h>
#define NULL 0
struct tree
{
	struct tree *alp[26];
	int count;
	bool end;
};

int main()
{
	//freopen("data.txt","r",stdin);
	int i,j,k,g;
	char str[42];
	char all[20000][42];
	int flag[42],end[42];
	struct tree *cur,*next,*head;
	head=new struct tree;
	for(i=0;i<26;i++)
		head->alp[i]=NULL;
	head->count=0;
	head->end=0;
	k=0;
	while(scanf("%s",str)!=EOF)		
	{
		strcpy(all[k++],str);
		cur=head;
		for(j=0;j<strlen(str);j++)
		{
			if(cur->alp[str[j]-'a']!=NULL)		//用树储存;
			{
				cur->count++;
				cur=cur->alp[str[j]-'a'];
			}	
			else
			{	
				next=new struct tree;
				for(i=0;i<26;i++)
					next->alp[i]=NULL;
				next->count=0;
				next->end=0;
				cur->alp[str[j]-'a']=next;
				++(cur->count);
				cur=next;
			}
		}
		cur->end=1;
	}
	for(i=0;i<k;i++)
	{
		printf("%s ",all[i]);
		memset(flag,0,sizeof(flag));
		memset(end,0,sizeof(end));
		j=0;
		cur=head->alp[all[i][0]-'a'];
		while(j<strlen(all[i]))
		{
			if(cur->end==1)
				end[j]=1;
			flag[j++]=cur->count;
			cur=cur->alp[all[i][j]-'a'];
		}
		for(j=strlen(all[i])-1;j>=0;j--)
		{
			if(flag[j]!=1&&flag[j]!=0)			//一般情况
			{
				for(g=0;g<=j+1;g++)
				{
					if(all[i][g]=='\0')
						break;
					printf("%c",all[i][g]);
				}
				break;
			}
			if(end[j]==1&&flag[j]!=0)			//非car遇car 和 car型
			{
				for(g=0;g<=j+1;g++)
				{
					if(all[i][g]=='\0')
						break;
					printf("%c",all[i][g]);
				}
				break;
			}
			if(j==0)								//首字母是唯一的
				printf("%c",all[i][0]);
		}
		printf("\n");
	}
	return 0;
	
}



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