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. JQuery中关于浏览器兼容性的问题

      前  言 LIUWE JQuery是一个特别强大的javascript代码库,,它的操作DOM的能力是相当强大的,JQuery可以说是支持各大主流浏览器,但是随着时代的不断发展,浏览器是在不断的更 ...

  2. 协议端口号(protocol port number)

    协议端口号(protocol port number) 先来个注意事项 (-> ->) 这种在协议层间的抽象的协议端口是软件端口,和硬件端口是完全不同的概念.硬件端口是不同设备进行交互的接 ...

  3. 如何从两个List中筛选出相同的值

    问题 现有社保卡和身份证若干,想要匹配筛选出一一对应的社保卡和身份证. 转换为List socialList,和List idList,从二者中找出匹配的社保卡. 模型 创建社保卡类 /** * @a ...

  4. 翻译连载 | 第 10 章:异步的函数式(上)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  5. Find 找规律,递推

    Find Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...

  6. GCD hdu2588

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. 【转】Python装饰器与面向切面编程

    原文请参考: http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html 今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切 ...

  8. C#中 什么是装箱和拆箱

    装箱:将值类型包装为引用类型 拆箱:将引用类型转换为值类型   例如 objetct obj = null; obj = ; //装箱 int i = (int) obj; //拆箱

  9. HDU3336 Count the string

    居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...

  10. ORALCE PL/SQL学习笔记

    ORALCE  PL/SQL学习笔记 详情见自己电脑的备份数据资料