题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat)

思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问。

代价:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=50005;
const int INF=1e9;
using namespace std;
char s[N][55];
struct Trie{
int num;
Trie *next[26];
Trie(){
num=0;
for(int i=0;i<26;i++){
next[i]=NULL;
}
}
};
Trie *root; void insert(char s[]){
int len=strlen(s);
Trie *p=root;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(p->next[v]==NULL){
p->next[v]=new Trie();
}
p=p->next[v];
}
if(!p->num){
p->num=1;
}
}
int query(char s[],int rt){ //rt代表这是前半个单词还是后半个
int len=strlen(s),v;
Trie *p=root;
for(int i=0;i<len;i++){
v=s[i]-'a';
if(p->next[v]!=NULL){ //注意加这一步,防止RE
p=p->next[v];
}
else return 0;
if(rt==1 && p->num && i!=len-1){ //rt==1才能查看是否能由两个单词拼接
if(query(s+i+1,2)){
return -1;
}
}
}
return p->num;
}
/*void del(Trie *p){
if(p==NULL) return;
for(int i=0;i<26;i++){
if(p->next[i]!=NULL) del(p->next[i]);
}
delete p;
}*/
int main(){
int cnt=0;
int flag;
root=new Trie();
while(scanf("%s",s[cnt])!=EOF){
insert(s[cnt++]);
}
for(int i=0;i<cnt;i++){
flag=0;
flag=query(s[i],1);
if(flag==-1){
printf("%s\n",s[i]);
}
}
return 0;
}

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(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

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

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

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

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

  9. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

随机推荐

  1. sql两列相除,保留n位小数

    ), ) from tablename 以上代码意思两列相处,然后保留4位小数.

  2. webview与js交互(转)

    原文:http://www.cnblogs.com/vanezkw/archive/2012/07/02/2572799.html 对于android初学者应该都了解webView这个组件.之前我也是 ...

  3. Memory consumption of popular Java data types

    http://java-performance.info/memory-consumption-of-java-data-types-2/ 如何在Java中分配超过-Xmx限制的内存 http://i ...

  4. google浏览器mac上跨域问题解决

    open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/ /Use ...

  5. Python中的__init__.py的作用

    当用 import 导入该目录时,会执行 __init__.py 里面的代码 因此在__init__.py文件中,把深层的包的路径缩短,别的地方就可以在引用到目录级别时引到深层的包.

  6. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  7. PAT 1072 Gas Station[图论][难]

    1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...

  8. 不要提交代码到HEAD上

    昨天为了修改代码,所以checkout 当时打包的分支,然后定位修改,但是发现自动切换为HEAD分支,没有在意,发现提交后,代码消失了. 然后怎么找也找不到了.什么git branch , git l ...

  9. jQuery在iframe里取得父窗口的某个元素的值

    提供一款jQuery在iframe里取得父窗口的某个元素的值实现,这个iframe用js也差不多,有需要的朋友可以参考一下. 1.在父窗口中获取指定iframe(testiframe) id 为 te ...

  10. linux 引导流程二

    grep -v  “^#” /etc/inittab | more 提取etc文件中的有效行. 用命令man 可以获得配置文件和命令的帮助信息.配置文件必须是系统的配置文件或系统默认安装的某个服务的配 ...