| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
大牛帮帮忙, sample都能过,提交总是RE,#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator