zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【算法笔记】B1020 月饼
    JZOJ 3412. 【NOIP2013模拟】KC看星
    JZOJ 3517. 空间航行
    JZOJ 3515. 软件公司
    JZOJ 3514. 最小比例
    JZOJ 3490. 旅游(travel)
    luogu P3178 [HAOI2015]树上操作
    JZOJ 3427. 归途与征程
    JZOJ 3426. 封印一击
    JZOJ 3425. 能量获取
  • 原文地址:https://www.cnblogs.com/liuxin13/p/3905102.html
Copyright © 2011-2022 走看看