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]);
    n = i;
    for(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/3905102.html