首页 > 其他 > 详细

hdu-1247

时间:2016-02-17 20:52:29      阅读:166      评论:0      收藏:0      [点我收藏+]

思路:

字典树模板题


#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 500007
using namespace std;

struct node
{
    int e;
    struct node* next[26];
};
node* root;
char str[MAX][50];

void Insert(char* s)
{
    node* ptr = root;
    for(;*s != \0;s++)
    {
        int n = *s-a;
        if(ptr->next[n] == NULL)
            ptr->next[n] = new node();
        ptr = ptr->next[n];
    }
    ptr->e = 1;
}

bool find2(char* s)
{
    node* ptr = root;
    for(;*s != \0;s++)
    {
        int n = *s-a;
        ptr = ptr->next[n];
        if(ptr != NULL) {
            if(ptr->e==1 && *(s+1)==\0)
                return true;
        }
        else
            return false; 
    }
    return false;
}

bool find1(char* s)
{
    node* ptr = root;
    for(;*s != \0;s++)
    {
        int n = *s-a;
        ptr = ptr->next[n];
        if(ptr->e==1 && find2(s+1))
            return true;
    }
    return false;
}

int main()
{
    int i = 0;
    root = new node();
    while(scanf("%s",str[i]) != EOF) {
        Insert(str[i]);
        i++;
    }
    for(int j = 0;j < i;j++)
        if(find1(str[j]))
            cout<<str[j]<<endl;
    return 0;
}

 

hdu-1247

原文:http://www.cnblogs.com/immortal-worm/p/5196406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!