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 ...
随机推荐
- Chisel语言
1 What is Chisel? Chisel(Constructing Hardware In a Scala Embedded Language)是一种嵌入在高级编程语言Scala的硬 ...
- spring 配置多个properties
复制多份,保证有效的配置文件,属性时true就行 <bean class="org.springframework.beans.factory.config.PropertyPlace ...
- POJ 1655 Balancing Act (树的重心,常规)
题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...
- 使用python模拟登陆百度
#!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...
- eclipse 的project explorer问题,这个怎样把localFileSystem去掉,
这个非常简单 把勾去掉就可以了
- 背包问题2 (lintcode)
这里: for(int j = 1;j <= m;j++) result[0][j] = 0x80000000; 不能从0开始,result[0][0]是可以取到的,是0.其他情况取不到才用最小 ...
- Linux网络管理及基础设置
一.网络管理 1 临时配置网络(ip,网关,dns) 用ifconfig命令设定网卡的IP地址: ens33网卡的IP地址为192.168.16.154, ifconfig ens33 192.168 ...
- CPP-练习
HW: 1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" ;局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会 ...
- hrbust-1545-基础数据结构——顺序表(2)
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) ...
- Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method
在使用SpringMVC的时候遇到了这个问题 问题原因: 在指定方法所对应的url地址的时候重复了, 也就是@RequestMapping("url")中, 两个方法使用了同一个 ...