HDU 1247 Hat’s Words(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247
题意:
给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成。
思路:
先将所有单独插入字典树,然后对于每一个单词,枚举它的分割点,去字典树中查找是否具有这两个单词。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = +; char s[maxn][];
int num = ; struct Trie
{
int son[];
int ends;
}t[maxn*]; void init(int x)
{
t[x].ends = ;
memset(t[x].son,,sizeof(t[x].son));
} void insert(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(!t[u].son[c])
{
num++;
init(num);
t[u].son[c] = num;
}
u = t[u].son[c];
}
t[u].ends = ;
} bool query(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(!t[u].son[c]) return false;
u = t[u].son[c];
}
if(t[u].ends == ) return true;
return false;
} int main()
{
//freopen("in.txt","r",stdin);
int tot = ;
char s1[],s2[];
while(~scanf("%s",s[tot++])) insert(s[tot-]);
for(int i=;i<tot;i++)
{
int len = strlen(s[i]);
for(int j=;j<len;j++)
{
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
strncpy(s1,s[i],j);
strncpy(s2,s[i]+j,len-j);
if(query(s1) && query(s2)) {printf("%s\n",s[i]);break;}
}
}
return ;
}
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 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(字典树,经典题)
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 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- flask 在视图函数中验证表单
在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...
- c# 静态方法和数据
c#所有方法都必须在类的内部声明,但如果把方法或者字段声明为static就可以使用,类名代用方法或者访问字段. 在方法中声明一个静态变量a 和一个静态的aFun方法.下面是在主函数中调用. 从上图可以 ...
- 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法
抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...
- php Allocator Jemalloc TCMalloc那个内存分配器比较好?
php Allocator Jemalloc TCMalloc那个内存分配器比较好? php一键安装脚本可以选择是否安装内存优化 You have 3 options for your Memory ...
- 高性能NIO框架Netty入门篇
http://cxytiandi.com/blog/detail/17345 Netty介绍 Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具 ...
- POJ 3320 Jessica's Reading Problem (尺取法)
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...
- 自写Jquery插件 Menu
原创文章,转载请注明出处,谢谢!https://www.cnblogs.com/GaoAnLee/p/9067543.html 可以结合我自写的Jquery插件Tab 一起使用哦 上一个整体效果 直接 ...
- 原生js上传图片时的预览
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- sql server 游标的简单用法
sql server游标: --定义游标 declare cursor1 cursor for select ID,Name from A --打开游标 open cursor1 declare @i ...
- win10系统jdk安装和环境变量配置
新换电脑的原因,要重新安装jdk,完整记录一下安装过程 jdk版本用的1.7(公司默认版本) 这是jdk安装目录 更改为D:\jdk\java\jdk1.7 安装jre目录 更改为D:\jdk\ ...