您的位置:首页 > 其它

hdu 1247 Hat’s Words

2012-08-13 15:05 281 查看

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3666 Accepted Submission(s): 1390



[align=left]Problem Description[/align]
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

You are to find all the hat’s words in a dictionary.

[align=left]Input[/align]
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.

Only one case.

[align=left]Output[/align]
Your output should contain all the hat’s words, one per line, in alphabetical order.

[align=left]Sample Input[/align]

a
ahat
hat
hatword
hziee
word


[align=left]Sample Output[/align]

ahat
hatwordtrie树很明显,但是不知道有没有相同的单词输入,如下面,结果证明是没有这样的数据,否则还要记录相同元素数据的个数╮(╯▽╰)╭赤果果的多虑了, inputc
cc
cc
ccccoutputcccc[code]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define begin freopen("a.in","r",stdin); freopen("a.out","w",stdout);
#define end   fclose(stdin); fclose(stdout);

using namespace std;
struct node
{int flag,use;
struct node *next[26];
node()
{flag=0; use=0;
memset(next,NULL,sizeof(next));
}
}root;
char s[50001][50];
void insert(char *s)
{int k=0;
struct node *p=&root;
while (s[k]!='\0')
{if (p->next[s[k]-'a']==NULL)
p->next[s[k]-'a']=new node;
p=p->next[s[k]-'a'];
++k;
}
p->flag=1; ++p->use;
};
int find(char *s)
{int k=0;
struct node *p=&root;
while ((s[k]!='\0')&&(p->next[s[k]-'a']!=NULL))
{p=p->next[s[k]-'a'];
++k;
}
if (s[k]=='\0') return p->flag;
return 0;
};
int main()
{int l,i,j,sum=0,f1,f2;
char s1[50],s2[50];

while (gets(s[++sum]))
insert(s[sum]);
for (i=1;i<=sum;i++)
{l=strlen(s[i]);
for (j=1;j<l;j++)
{s1[j]='\0'; s2[l-j]='\0';
strncpy(s1,s[i],j);
strncpy(s2,s[i]+j,l-j);
if (find(s1)&&find(s2)) {printf("%s\n",s[i]); break;}
}
}

return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: