HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来。于是就换了如今的数组。即,把每个输入的字符串都存入二维数组中,然后创建字典树。输入和创建完成后,開始查找。
事实上一開始就读错题目了,题目要求字符串是由其它两个输入的字符串组成的前后缀,自己根本没有推断前缀是否满足。就直接推断后缀,一直想不通自己哪里错了,非常羞愧,水平还是不行。
查找分为前缀查找和后缀查找。事实上两个函数基本差点儿相同的。以下放代码。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; struct trie
{
trie *next[26];
int v; //字符同样的个数
trie()
{
memset(next,NULL,sizeof(next));
v=1;
}
}; trie *root=new trie();
char s[50001][101]; void creat_trie(char *str)
{
int i,id;
trie *p;
for(p = root,i=0;str[i]; ++i)
{
id = str[i]-'a';
if(p->next[id] == NULL)
{
p->next[id] = new trie();
}
p = p->next[id];
}
p->v = -1;
} int find_trie(char *str)
{
int i=0,j,id;
trie *p = root;
for(;*str != '\0';)
{
id= *str - 'a' ;
if (p->next[id] != NULL)
{
p = p->next[id];
if(p->v == -1 && *(str+1) == '\0')
return 1;
str++;
}
else
return 0;
}
return 0; } int find(char *str)
{
trie *p = root;
int m;
for (;*str != '\0';)
{
m = *str - 'a';
p = p->next[m];
if(p != NULL)
{
if (p->v == -1 && find_trie(str+1))
{
return 1;
}
str++;
}
else
return 0; }
return 0;
} int main()
{
int N,n,i=0,j,t,m,flag=0;
int a,b,c,d,k;
int sum;
trie *p;
while (gets(s[i]),s[i][0] != '\0')//,s[i][0] != '\0'
{
creat_trie(s[i++]);
}
for (j=0;j<i;j++)
{
if (find(s[j]))
{
puts(s[j]);
}
}
return 0;
}
代码2:map容器
#include <iostream>
#include <map>
#include <cstring>
using namespace std; map <string,int> m;
string s[50005]; int main()
{
int k=-1;
while(cin>>s[++k])
{
m[s[k]] = 1;
}
for(int i=0;i<=k;i++)
{
int len = s[i].length();
for(int j=1;j<len;j++)
{
string s1(s[i],0,j); //从0開始的j个数
string s2(s[i],j); //从j開始(不包含)一直到结尾
if(m[s1] == 1 && m[s2] == 1)
{
cout<<s[i]<<endl;
break;
} }
}
return 0;
}
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9436 Accepted Submission(s): 3369
You are to find all the hat’s words in a dictionary.
Only one case.
a
ahat
hat
hatword
hziee
word
ahat
hatword
HDU 1247 Hat’s Words (字典树 && map)的更多相关文章
- 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 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 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 1247 Hat’s Words(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- ubuntu python opencv3 cv2.cv2 has no attribute 'face' 'cv2.face' has no attribute 'createEigenFaceRecognizer'
学习opencv过程中遇到错误: 1 cv2.cv2 has no attribute 'face' 经过一顿查,,,各种走弯路 最后一下子就解决了: pip install opencv-pyth ...
- 线性表之单链表C++实现
线性表之单链表 一.头文件:LinkedList.h //单链表是用一组任意的存储单元存放线性表的元素,这组单元可以是连续的也可以是不连续的,甚至可以是零散分布在内存中的任意位置. //单链表头文件 ...
- codevs 1005 生日礼物
1005 生日礼物 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 9月12日是小松的朋友小寒的生日.小松知 ...
- POJ Anniversary party 树形DP
/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...
- Alpha冲刺(4/10)——追光的人
1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...
- List常用子类的特点
ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全, 效率较高 Vector 底层数据结构是数组,查询快,增删慢 线程安全, 效率较低 LinkedList 底 ...
- gcc 内联汇编
http://www.cnblogs.com/zhuyp1015/archive/2012/05/01/2478099.html
- LeetCode152:Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- setTimeout() 实现程序每隔一段时间自己主动运行
定义和使用方法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 參数 描写叙述 code 必需.要调用的函数后要运行 ...
- 汉字转拼音的Java类库——JPinyin
原文:http://blog.csdn.net/stuxuhai/article/details/8932715 [JPinyin主要特性]1.准确.完善的字库:Unicode编码从4E00-9FA5 ...