题目大意:

hat's word 的定义是字典中 恰好由另外两个单词连接起来的单词

给你一本字典,问有多少个hat‘s word,(字典按字典序给出)

单词数50000..

初步思路:

单词分为前缀单词,后缀单词

前缀单词出现在字典的前面,后缀单词出现在字典后面?

1.枚举前缀,哈希判断后缀?

复杂度:N^N*单词平均长度,显然不靠谱

2.trie树?

先建树,然后对于每一个单词读入,如果经过了某些单词结尾,判断一下后缀有没有.

复杂度似乎可靠?写写吧....

代码写成功..样例过..RE..字典树过大?.,..RE无数发 发现

int find_trie(char *s)
{
trie *root=TREE;
for(int i=0;s[i];i++)
{
int t=s[i]-'a';
if(root->next[t]==NULL) return 0;//写成root->next==NULL root=root->next[t];
}
if(root->flag==1) return 1;
else return 0;
}
</pre><p></p><p><span style="font-size:24px">已A.</span></p><p><span style="font-size:24px"></span></p><pre code_snippet_id="658096" snippet_file_name="blog_20150503_3_3616348" name="code" class="cpp">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
char buffer[50005][205];
struct trie{
trie *next[30];
int flag;
}TREE[200000],*EE;
void insert_trie(char *s)
{
trie *root=TREE;
for(int i=0;s[i];i++)
{
int t=s[i]-'a';
if(root->next[t]==NULL) { root->next[t]=EE++;}
root=root->next[t];
}
root->flag=1;
}
int find_trie(char *s)
{
trie *root=TREE;
for(int i=0;s[i];i++)
{
int t=s[i]-'a';
if(root->next[t]==NULL) return 0;
root=root->next[t];
}
if(root->flag==1) return 1;
else return 0;
}
void solve_trie(char *s)
{
int ok=0;
trie *root=TREE;
for(int i=0;s[i];i++)
{
int t=s[i]-'a';
root=root->next[t];
if(root->flag==1)
if(find_trie(s+i+1)==1)
{
ok=1;
break;
}
}
if(ok) printf("%s\n",s);
}
void Clear()
{
memset(TREE,0,sizeof(TREE));
EE=TREE+1;
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
int p=0;
// init();
Clear();
while(scanf("%s",buffer[++p])!=EOF)
insert_trie(buffer[p]);
for(int i=1;i<=p;i++)
{
solve_trie(buffer[i]);
}
return 0;
}

【Trie】【HDU1247】【Hat’s Wordsfd2】的更多相关文章

  1. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  2. 【JQ+锚标记实现点击页面回到顶部】

    前言:今天想写个页面常用到的[点击回到页面顶部或是首页的功能],生活和职场一样,总会有低谷的时候,这个时候咱也别怂.别怂.别怂,说三遍!那都不是事,工作没了,再找呗,就像我上周五,团队解散那天,我是笑 ...

  3. 【小白的CFD之旅】13 敲门实例【续3】

    接上文[小白的CFD之旅]12 敲门实例[续2] 4 Results4.1 计算监测图形4.2 Graphics4.2.1 壁面温度分布4.2.2 创建截面4.2.3 显示截面物理量4.2.4 Pat ...

  4. [No00006D]下载离线版的github for windows【以Github for Windows 3.0.110.为例】

    目录 先上地址后讲原理: 原理: 11个目录的文件怎么一口气下载呢? 最后,把下好的文件批量名,同时将GitHub.exe.manifest也放到软件根目录下(与GitHub.exe同级): 今后的猜 ...

  5. Tp验证码:$Verify = new \Think\Verify(); $Verify->entry(n);【参数n,页面有多个验证码时用】

    一.验证码参数:(中文字符集和英文字符集在父类里面都可以取到,可修改) //1.生成验证码 $Verify = new \Think\Verify(); $Verify->entry(n);[参 ...

  6. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。

    [SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接  http://www.4gamer.net/ ...

  7. 【转】iOS静态库 【.a 和framework】【超详细】

    原文网址:https://my.oschina.net/kaqijiang/blog/649632 一.什么是库? 库是共享程序代码的方式. 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存 ...

  8. 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)

    [概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...

  9. 转载:【高并发简单解决方案 | 靠谱崔小拽 】redis队列缓存 + mysql 批量入库 + php离线整合

    需求背景:有个调用统计日志存储和统计需求,要求存储到mysql中:存储数据高峰能达到日均千万,瓶颈在于直接入库并发太高,可能会把mysql干垮. 问题分析 思考:应用网站架构的衍化过程中,应用最新的框 ...

随机推荐

  1. Beauty of Array(模拟)

    M - M Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status P ...

  2. SQL With(递归 CTE 查询)

    本文来自:http://www.cnblogs.com/smailxiaobai/archive/2012/01/16/2323291.html 指定临时命名的结果集,这些结果集称为公用表表达式 (C ...

  3. Kali下使用libheap

    Kali下使用libheap 在github上,可以libheap用来帮助调试堆溢出.链接见:https://github.com/cloudburst/libheap 但是最后一次更新在一年前了,我 ...

  4. 【特殊的图+DP】【11月校赛】大家一起玩游戏

    大家一起玩游戏 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  5. js 写日期

    <SCRIPT language="JavaScript" type="text/JavaScript">   today = new Date() ...

  6. Reverse Integer (JAVA)

    public class Solution { public int reverse(int x) { StringBuffer sb = new StringBuffer(x+"" ...

  7. Android Studio默认产生Fragment

    package com.edaixi.fragment; import android.content.Context;import android.net.Uri;import android.os ...

  8. 资源回收 left

    select DISTINCT human.tid,log_pv_change.systafftid from human left join human_user on human.tid=huma ...

  9. 为什么我们需要性能测试,需要loadrunner

    什么是功能: 功能按我理解就是软件实现需求,提供服务,那么功能测试就是实现的需求是否与客户给定需求一致,也就是符合预期结果 什么是性能: 功能是实现需求,提供服务,那么性能就可以理解为服务的好坏.比如 ...

  10. ServletConfig和ServletContext

    ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...