hdu 1247 Hat’s Words
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3431    Accepted Submission(s): 1303
You are to find all the hat’s words in a dictionary.
Only one case.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define N 60000
#define Size 26
using namespace std;
struct Tire
{
  bool isword;
  Tire *next[Size];
}node [N];
char s[50000][20];
int nu;
void insert(Tire *&root,char *word)
{
    Tire *p=root;
    while(*word)
    {
        if(p->next[*word-'a']==NULL)
            p->next[*word-'a']=&node[++nu];
         p=p->next[*word-'a'];
        word++;
    }
    p->isword=true;
}
bool ask(Tire *root,char *word)
{
    Tire *p=root;
    while(*word)
    {
        if(p->next[*word-'a']==NULL)
          return false;
        p=p->next[*word-'a'];
      word++;
    }
    if(p->isword) return true;
    return false;
}
bool query(Tire *root,char *word)
{
    Tire *p=root;
    while(*word)
    {
        if(p->next[*word-'a']==NULL)
          return false;
        p=p->next[*word-'a'];
        if(p->isword&&ask(root,word+1))
           return true;
      word++;
    }
    return false;
}
int main()
{
    int n=-1;
    Tire *root=&node[0];
    root->isword=false;
    while(scanf("%s",s[++n])!=EOF)
    {
       insert(root,s[n]);
    }
    for(int i=0;i<=n;i++)
     if(query(root,s[i]))
      printf("%s\n",s[i]);
    return 0;
}
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号