Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13798    Accepted Submission(s): 4950

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
hatword
 
Author
戴帽子的
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1075 1298 1800 2846 1305 
/*
一开始想到将单词分割,但是怕超时,后来一想单词长度可能10^2差不多,10^7的运行时间应该不会超时
*/
#include<bits/stdc++.h>
#define N 30
using namespace std;
#define MAX 26
const int maxnode=*+;///预计字典树最大节点数目
const int sigma_size=;///每个节点的最多儿子数 struct Trie
{
///这里ch用vector<26元素的数组> ch;实现的话,可以做到动态内存
int ch[maxnode][sigma_size];///ch[i][j]==k表示第i个节点的第j个儿子是节点k
int val[maxnode];///val[i]==x表示第i个节点的权值为x
int sz;///字典树一共有sz个节点,从0到sz-1标号 ///初始化
void Clear()
{
sz=;
memset(ch[],,sizeof(ch[]));///ch值为0表示没有儿子
} ///返回字符c应该对应的儿子编号
int idx(char c)
{
return c-'a';
} ///在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
///所以执行Insert前需要判断一下是否已经存在s单词了
void Insert(char *s)
{
int u=,n=strlen(s);
for(int i=;i<n;i++)
{
int id=idx(s[i]);
if(ch[u][id]==)///无该儿子
{
ch[u][id]=sz;
memset(ch[sz],,sizeof(ch[sz]));
val[sz++]=;
}
u=ch[u][id];
}
val[u]=n;
} ///在字典树中查找单词s
bool Search(char *s)
{
int n=strlen(s),u=;
for(int i=;i<n;i++)
{
int id=idx(s[i]);
if(ch[u][id]==)
return false;
u=ch[u][id];
}
return val[u];
}
};
Trie trie;///定义一个字典树
char op[][N];
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int len=;
trie.Clear();
while(gets(op[len++]))
trie.Insert(op[len-]);
//cout<<len<<endl;
for(int i=;i<len-;i++)
{
int n=strlen(op[i]);
//cout<<"op[i]="<<op[i]<<" i="<<i<<endl;
for(int k=;k<n;k++)
{
char str1[N],str2[N];
strncpy(str1,op[i],k);
str1[k]='\0';
strncpy(str2,op[i]+k,n);
str2[n-k]='\0';
if(trie.Search(str1)&&trie.Search(str2))
{
puts(op[i]);
break;//这一句是必须加的因为不加就会输出两遍
}
//cout<<str1<<" "<<str2<<endl;
}
//puts(op[i]);
}
return ;
}

Hat’s Words的更多相关文章

  1. KALI Linux problems & Study Red Hat | Ubuntu

    Problem When you ask some website with https head.you may met the problem  secure connection failed ...

  2. Red Hat Enterprise Server 6.0 安装Sendmail相关包

    由于需要在Linux服务器(Red Hat Enterprise Linux Server release 6.0)上配置邮件服务,需要安装Sendmail包,一般Sendmail的安装有两种方式:R ...

  3. Red Hat Enterprise Linux 6.6安装体验

    Red Hat Enterprise Linux 6.6的安装首界面有五个选项,这跟以前的Red Hat Enterprise Linux 5.x的安装界面是有一些区别的.   安装或者升级现有系统( ...

  4. Red Hat Enterprise Linux 各个版本以及发布日期

    Red Hat Enterprise Linux 7 Release/Update General Availability Date redhat-release Errata Date* Kern ...

  5. linux red hat 给普通用户开启root权限

    环境:虚拟机:red hat 6.5:root角色用户:普通用户:宏基笔记本:win7: 操作过程: 1.登录普通用户,进入图形界面(可以设置为启动登录进入命令行界面): 2.按Crl+ALT+F2进 ...

  6. 使用 KGDB 调试 Kernel On Red Hat Linux

    1. KGDB 简介         KGDB  提供了一种使用 GDB 调试 Linux 内核的机制.使用 KGDB 可以象调试普通的应用程序那样,在内核中进行设置断点.检查变量值.单步跟踪程序运行 ...

  7. Red hat 6.4下面的qt安装

    运行环境:Red hat 6.4 去官网下载qt5.2并且安装 当启动的时候会出现如下错误 核心载入失败: /opt/Qt5.2.0/Tools/QtCreator/lib/qtcreator/plu ...

  8. Red Hat Enterprise Server 5.8+oracle10g(中文界面)安装

    Red Hat Enterprise Server 5.8+oracle10g(中文界面)安装 VMware workstation10(虚拟机)下面安装红帽企业版5.8 创建虚拟机 新建虚拟机,选择 ...

  9. 如何安装win10+Red Hat Enterprise Linux双系统?

    1,如何安装win10+Red Hat Enterprise Linux双系统???? 有很多人(没做过调查,可能就我自己想装吧)想要安装Red Hat Enterprise Linux系统,但是又不 ...

  10. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

随机推荐

  1. AngularJS -- 指令(创建自定义指令)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/   什么是指令 注:本指南是针对已经熟悉AngularJS基础知识的开发人员.如果你才刚 ...

  2. 第6章 Overlapped I/O, 在你身后变戏法 ---Win32 文件操作函数 -2

    Win32 之中有三个基本的函数用来执行 I/O,它们是:        i CreateFile()        i ReadFile()        i WriteFile()    没有另外 ...

  3. Football 概率DP poj3071

                                                                                                 Footbal ...

  4. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

  5. bzoj1806 [Ioi2007]Miners矿工配餐

    [bzoj1806][Ioi2007]Miners 矿工配餐 2014年7月10日1,7870 Description 现有两个煤矿,每个煤矿都雇用一组矿工.采煤工作很辛苦,所以矿工们需要良好饮食.每 ...

  6. MySQL之增删改查

    前言:以下是MySQL最基本的增删改查语句,很多IT工作者都必须要会的命令,也是IT行业面试最常考的知识点,由于是入门级基础命令,所有所有操作都建立在单表上,未涉及多表操作. 前提:在进行" ...

  7. sql sever 基础知识及详细笔记

    第六章:程序数据集散地:数据库 6.1:当今最常用的数据库 sql  server:是微软公司的产品 oracle:是甲骨文公司的产品 DB2:数据核心又称DB2通用服务器 Mysql:是一种开发源代 ...

  8. XSS跨站脚步攻击及防范

    XSS(Cross Site Script)跨站脚本攻击.它指的是恶意攻击者往Web 页面里插入恶 意html 代码,当用户浏览该页之时,嵌入其中Web 里面的html 代码会被执行,从而达到侵害用户 ...

  9. JAVAWEB复习资料-01

    CSS中@import和link两种插入样式表方式有什么不同? 1.link属于HTML标签,除了引入css文件之外还能定义RSS等,而@import只能用于加载CSS. 2.link在引用CSS时, ...

  10. jQuery中的常用内容总结(二)

    jQuery中的常用内容总结(二) 转载请注明地址: http://www.cnblogs.com/funnyzpc/p/7571993.html 前言 距离上次博客更新已经有二十来天了(●′ω`●) ...