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

      前  言 xiaoq AngularJS 通过新的属性和表达式扩展了 HTML. 使用起来非常方便. 1. AngularJS的指令与表达式 AngularJS 通过 指令 扩展了 HTML,且通 ...

  2. PHPFastCGI进程管理器PHP-FPM详解

    PHP-FPM是一个PHPFastCGI进程管理器,是只用于PHP的.      PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将它patch到你的PH ...

  3. 配置exVim开发环境

    exVim主页 http://exvim.github.io/ 使用该配置原因: 简单,组织各种优秀插件,安装包很小,各种操作很流畅 实用,对于项目来说,只需要多出一个xx.exvim文件,所有符号等 ...

  4. AngularJS的$rootScope和$scope联系和区别

    scope是html和单个controller之间的桥梁,数据绑定就靠他了. rootscope是各个controller中scope的桥梁.用rootscope定义的值,可以在各个controlle ...

  5. Writing Science 笔记 6.20

    1.写作的六个要素:S: Simple 简单的 U: Unexpected 出人意料的 C: Concrete 具体的  C: Credible 可信的  E: Emotional S: Storie ...

  6. java集合系列——Map介绍(七)

    一.Map概述 0.前言 首先介绍Map集合,因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的). 1:介绍 将键映射到 ...

  7. linux debian 9 配置postgresSQL数据库

    #读者注意:本文可以选择不看解释,直接执行每段的0中的代码 (〇):一些概念(可以跳过直接使用(一)0的代码) 1. 客户端:psql.postgreSQL的命令行客户端程序,在终端输入psql进入p ...

  8. vue2组件之select2调用

    目前,项目中使用了纯前端的静态项目+RESTFul接口的模式.为了更好的对数据进行操作,前端使用了vue2的mvvm功能,但是由于不是单页面应用,所以,并没有涉及到其它的如vue-route等功能,也 ...

  9. zoj1797 Least Common Multiple 最小公倍数

    Least Common Multiple Time Limit: 2 Seconds      Memory Limit: 65536 KB The least common multiple (L ...

  10. Linux中创建Daemon进程的三种方法

    什么是daemon进程? Unix/Linux中的daemon进程类似于Windows中的后台服务进程,一直在后台运行运行,例如http服务进程nginx,ssh服务进程sshd等.注意,其英文拼写为 ...