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

#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. rdlc报表

    也是第一次接触报表这个东西.现在在我理解,报表无非就是两个内容,格式和数据. 格式没有多少了解,就记录了,以后再续.数据的绑定和结果的显示是怎么实现的呢? 今天的主角就是rdlc这个文件和Report ...

  2. SQL SERVER 中PatIndex的用法个人理解

    一般用法:PatIndex('%AAA%',‘BBBBBBBB’) 上句的意思是查找AAA在BBBBBBBB中的位置,从1开始计算,如果没有的话则返回0 其中%AAA%的用法和 SQL语句中like的 ...

  3. string相关

    1.find相关 string s="abcd"; size_t pos0 = s.find_first_of("dcb");         1    //返 ...

  4. python模块之re正则表达式

    41.python的正则表达式      1. python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字      \W大写代表非\w ...

  5. 淘宝API举例子

    # -*- coding: utf-8 -*- import urllib2 import urllib import time import md5 secret='xx' app_key='yy' ...

  6. Python元组、列表、字典

    ```python>>> help(tuple) Help on class tuple in module __builtin__: class tuple(object) │ t ...

  7. UFLDL实验报告1: Softmax Regression

    PS:这些是今年4月份,跟斯坦福UFLDL教程时的实验报告,当时就应该好好整理的…留到现在好凌乱了 Softmax Regression实验报告 1.Softmax Regression实验描述 So ...

  8. 关于html5

    html5   是用来  将 js  和 css  结合起来 从而实现 各种功能 javascript 用来定义 html5   页面的逻辑 css 来定义 html5 中的显示样式

  9. 转:简单介绍 P3P 技术

    原文来自于:http://blog.csdn.net/ghj1976/article/details/4889219 以 Internet Explorer 为例,默认情况下,IE的隐私策略如下图所设 ...

  10. 【译】UI设计基础(UI Design Basics)--导航(Navigation)(六)

    [译]UI设计基础(UI Design Basics)--导航(Navigation)(六)