简单的字典树 - -,求一个单词是否由两个单词组成

#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\n", 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;
}

HDU 1247的更多相关文章

  1. hdu 1247 map的使用

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    ...

  2. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  3. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  4. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  5. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU 1247 Hat's Words (map+string)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

    题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algori ...

  8. HDU 1247 Hat’s Words (字符串匹配,暴力)

    题意: 给出一堆单词,如果有一个单词可以分成左右串两个单词,并且在所给的一堆单词中存在,就是hat词,统计所有这样的词,并按字典序输出. 思路: 注意定义,一个hat词可以被两部分已经存在的词组成,那 ...

  9. HDU 1247 Hat’s Words

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. hdu 1247 Hat’s Words(字典树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. 常用的dos命令之简略总结

    Dos常用命令  一.基础命令  1 dir  无参数:查看当前所在目录的文件和文件夹.  /s:查看当前目录已经其所有子目录的文件和文件夹.  /a:查看包括隐含文件的所有文件.  /ah:只显示出 ...

  2. Deep Learning 学习随记(四)自学习和非监督特征学习

    接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...

  3. cmd 命令行下复制、粘贴的快捷键

    1.单击左下角“开始”菜单,选择“运行”,输入“cmd”. 2.在弹出的cmd窗口的标题栏上点击“右键”,选择“属性”. 3.在弹出的对话框中选择“选项”这个选项卡,在“编辑选项”区域中勾选“快速编辑 ...

  4. CentOS6.5升级为CentOS7.0

    CentOS6.5升级为CentOS7.0 CentOS6.5升级为CentOS7 升级前: [root@localhost ~]# cat /proc/version  Linux version ...

  5. centos安装nodejs和mongodb

    安装nodejs: Run as root on RHEL, CentOS or Fedora, for Node.js v4 LTS Argon: curl --silent --location ...

  6. 入门3:PHP环境开发搭建(windows)

    一.环境需要 硬件环境(最低配置): 双核CPU 8G内存 操作系统环境: Windows(64位)7+ Mac OS X 10.10+ Linux 64位(推荐Ubuntu 14 LTS) /**拓 ...

  7. VIM快捷键(转)

    VIM快捷键:光标移动:四个方向   kh 0 l   j  ctrl+f, ctrl+b                 向下翻页,向上翻页  ctrl+d, ctrl+u              ...

  8. Object-C非正式协议与正式协议的区别

    Object-C非正式协议与正式协议的区别 这两个概念困扰我很久了,一直都很像搞清楚到非正式协议和正式协议有什么区别和联系,下面结合网上的资料和自己的看法谈谈这个问题. 一.非正式协议 显然这个名词是 ...

  9. 转:gpio_request

    今天再次学习SD卡驱动,遇到pgio_request这个函数,始终不知道其什么意思,看了几遍源代码才有了点感觉.现将其关键部分再此说明一下,以备自己以后复习,或是路客参考. 一般gpio_reques ...

  10. Comparing randomized search and grid search for hyperparameter estimation

    Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...