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

#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. ORACLE 中ROWNUM用法总结!(转)

    对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...

  2. angularjs 遇到Error: [$injector:unpr] Unknown provider: tdpicnews-serviceProvider <- tdpicnews-service <- tdpic-controller 错误

    define(['modules/tdpic-module', 'services/news-service', 'utilities/cryto'], function (app) { 'use s ...

  3. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  4. Xcode 使用自定义字体

    添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...

  5. Bootstrap_Javascript_提示框

    一. 结构分析 在Bootstrap框架中的提示框,结构非常简单,常常使用的是按钮<button>标签或者链接<a>标签来制作.不管是使用按钮还是链接来制作提示框,他们都有一个 ...

  6. PHP不依赖系统自动执行机制

    不依赖系统,以及不依赖yii事物机制,则考虑人为触发.触发可以写在总体的公共页面上,但是考虑到对数据库以及WWW服务器的压力问题,程序的延迟问题,需要对执行函数进行一些优化. 首先,我们考虑对数据库的 ...

  7. Day12 线程池、RabbitMQ和SQLAlchemy

    1.with实现上下文管理 #!/usr/bin/env python# -*- coding: utf-8 -*-# Author: wanghuafeng #with实现上下文管理import c ...

  8. 练习2 A - ASCII码排序

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 输入三 ...

  9. C++实现base64编码

    将昨天的php代码改造成C++ /*base_64.h文件*/ #ifndef BASE_64_H #define BASE_64_H /** * Base64 编码/解码 * @author lir ...

  10. E: Some packages could not be authenticated

    问题:          在Ubuntu上,安装软件时出现了“E: Some packages could not be authenticated”错误. 原因:     表示系统无法验证这个软件包 ...