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(字典树)的更多相关文章

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  2. hdu 1247 Hat’s Words(字典树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  4. 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 ...

  5. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  6. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  8. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...

  9. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. tensorflow学习4-过拟合-over-fitting

    过拟合: 真实的应用中,并不是让模型尽量模拟训练数据的行为,而是希望训练数据对未知做出判断. 模型过于复杂后,模型会积极每一个噪声的部分,而不是学习数据中的通用 趋势.当一个模型的参数比训练数据还要多 ...

  2. centos6使用yum安装python3和pip3

    在安装了epel源的情况下,直接yum就可以安装python3.4 #yum install python34 -y# python3 --versionPython 3.4.5 没有自带pip3,需 ...

  3. Django后端项目----restful framework 认证源码流程

    一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...

  4. iframe有那些缺点

    1.页面样式调试麻烦,出现多个滚动条: 2.浏览器的后退按钮失效: 3.过多会增加服务器的HTTP请求: 4.小型的移动设备无法完全显示框架: 5.产生多个页面,不易管理: 6.不容易打印: 7.代码 ...

  5. GoldenGate实时投递数据到大数据平台(6)– HDFS

    GoldenGate可以实时将RDBMS的数据投递到HDFS中,在前面的文章中,已经配置过投递到kafka, mongodb等数据平台,本文通过OGG for bigdata的介质中自带的示例演示实时 ...

  6. Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程

    https://blog.csdn.net/jywowaa/article/details/52263711 学习中用到深度学习的框架,需要搭建caffe.theano和torch框架.经过一个月的不 ...

  7. yii2 mysql根据多个字段的数据计算排序

    mysql根据多个字段的数据计算排序 select *,num1+num2*10+num3*100 num from $tableName order by num desc yii2框架活动记录ac ...

  8. zookeeper各种报错、原因及解决方法汇总(持续更新)

    [root@iZ23nn1p4mjZ zookeeper-3.4.10]# bin/zkCli.sh -server localhost:2181,localhost:2182,localhost:2 ...

  9. git review filter的一些规则

    https://review.openstack.org/#/c/258797/ https://review.openstack.org/#/q/is:closed+(owner:yuntong)+ ...

  10. tar 压缩指令基本用法

    压缩:tar -cjvf aaa.tar.bz2  www.test.com/  --exclude *.log(-j是用bz2压缩,-exclude是排除.log后缀的文件) c-创建 j-bzip ...