HDU 1247 解题报告

http://acm.hdu.edu.cn/showproblem.php?pid=1247

Hat’s Words

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

Problem Description

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.

Input

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.

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input

a
ahat
hat
hatword
hziee
word

Sample Output

ahat
hatword

CODE

#include
struct tree
{
  bool t;//记录以该节点作为结尾是否有单词存在
  tree *node[26];
  tree(){
      t=false;
      for(int i=0;i<26;++i)
	node[i]=NULL;
  }
} *root;
void insert(char *s)
{
  char c;
  tree *p=root;
  for(int i=0;s[i];++i){
      c=s[i]-'a';
      if(p->node[c]==NULL)
	p->node[c]=new tree();
      p=p->node[c];
  }
  p->t=true;
}
bool find(char *s)
{
  tree *p=root;
  char c;
  for(int i=0;s[i];++i){
      c=s[i]-'a';
      if(p->node[c])
	p=p->node[c];
      else
	return false;
  }
  return p->t;
}
void release(tree *p)
{
  for(int i=0;i<26;++i)
    if(p->node[i])
      release(p->node[i]);
  delete(p);
}
int main()
{
  int i,j,k,t,cnt=0;
  char l[20],r[20];
  char D[50000][20];
  root=new tree();
  while(gets(D[cnt++]))
    insert(D[cnt-1]);
  for(i=0;i
					
发表评论?

0 条评论。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>