HDu-1247 Hat’s Words,字典树裸模板!
Hat’s Words
题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词。
思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找。
目测数据不强,开始不知道单词长度都不敢下手了。。
struct tree
{
bool f;
tree *next[N];
tree()
{
for(int i=0;i<N;i++) next[i]=NULL;
f=false;
}
};
void insert(tree *root,char *s)
{
tree *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
tree *temp=new tree();
p->next[*s-'a']=temp;
}
p=p->next[*s-'a'];
s++;
}
p->f=true;
}
int find(tree *root,char *s)
{
tree *p=root;
while(p!=NULL&&*s!='\0')
{
p=p->next[*s-'a'];
s++;
}
if(p==NULL) return false;
return p->f;
}
void del(tree *root)
{
for(int i=0;i<N;i++)
if(root->next[i]!=NULL)
del(root->next[i]);
delete(root);
}
char s[50001][100],s1[100],s2[100];
int main()
{
tree *root=new tree();
int num=0;
while(~scanf("%s",s[num++]))
{
insert(root,s[num-1]);
}
for(int i=0;i<num;i++)
{
int len=strlen(s[i]);
for(int j=0;j<len;j++)
{
int l1=0,l2=0;
for(int k=0;k<j;k++) s1[l1++]=s[i][k];//分成两部分
for(int k=j;k<len;k++) s2[l2++]=s[i][k];
s1[l1]='\0',s2[l2]='\0';
if(find(root,s1)&&find(root,s2))
{
puts(s[i]);
break;
}
}
}
del(root);
return 0;
}
HDu-1247 Hat’s Words,字典树裸模板!的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Hdu 1247 Hat's Words(Trie树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- hdu1251+字典树常用模板
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- 起学习iOS开发专用词汇
今天的单词分别是: l Asynchronous 形容词 异步的 n 副词形式: asynchronously 异步地 n 缩写:ASYNC n 反义词:synchronous 形容词同步 ...
- Spring MVC系列[2]——参数传递及重定向
1.目录结构 2.代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
<Selenium简介>中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python.在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编 ...
- RunTests.sh && RunIPhoneSecurityd.sh
https://github.com/gh-unit/gh-unit/blob/master/Scripts/RunTests.sh #!/bin/sh # If we aren't ru ...
- Grace Huang 2017/1/11
原文 This actress becomes each character she plays Grace Huang has no interested in doing same thing y ...
- iOS打包上传app store各种问题解决总结
问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...
- 当互联网遇上家装,十大家装O2O混战
2015年已过去大半,装修O2O就出现了新的局面:为数众多的家居网络平台在家装O2O领域还未站稳脚跟,新的入局者就打出超低价格登场.新老O2O家装大战迅速展开,除了拼价格还拼品牌和体验,家装O2O的好 ...
- SSIS 通过 WINscp 从SFTP下载文件
1.通过SSIS的process task调用 winscp :C:\Program Files (x86)\WinSCP\WinSCP.exe /script="C:\SFTPFile\T ...
- Java数据结构面试题
1.栈和队列的共同特点是(只允许在端点处插入和删除元素) 4.栈通常采用的两种存储结构是(线性存储结构和链表存储结构) 5.下列关于栈的叙述正确的是(D) A.栈是非线性结构B.栈是一种树状 ...
- div section article区分--20150227
div section article ,语义是从无到有,逐渐增强的.div 无任何语义,仅仅用作样式化或者脚本化的钩子(hook),对于一段主题性的内容,则就适用 section,而假如这段内容可以 ...