Hat’s Words hdu-1247

就是查找这个单词能不能有两个单词组成,简单的字典树题目
//////////////////////////////////////////////////////////////
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

#define maxn 26

struct node
{
    int sum;
    node *next[26];
};

node *root;
char a[50005][30];

void Insert(char *s);//建立字典树
bool Find(char *s);
bool Query(char *s);

int main()
{
    int i, n=0;

    root = new node();

    for(i=0; scanf("%s", a[i]) != EOF; i++)
        Insert(a[i]);
   
    for(n=1, i=0; i<=n; i++)
    {
        if(Query(a[i]))
            printf("%s ", a[i]);
    }

    return 0;
}

void Insert(char *s)
{
    node *p = root;

    for(int i=0; s[i]; i++)
    {
        int k = s[i] - 'a';

        if(!p->next[k])
            p->next[k] = new node();
        p = p->next[k];
    }

    p->sum = 1;
}
bool Find(char *s)
{
    node *p = root;

    for(int i=0; s[i]; i++)
    {
        int k = s[i] - 'a';

        if(!p->next[k])
            return false;
        p = p->next[k];
    }

    if(p->sum)return true;
    return false;
}
bool Query(char *s)
{
    node *p = root;

    for(int i=0; s[i]; i++)
    {
        int k = s[i] - 'a';

        p = p->next[k];

        if(p->sum && Find(s+i+1))
            return true;
    }

    return false;

} 

原文地址:https://www.cnblogs.com/liuxin13/p/4674319.html