题目链接:

pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php?

pid=1247

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

You are to find all the hat’s words in a dictionary.
 
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.

Only one case.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
hatword

题意:

寻找出单次表中一个单词是由另外两个单词组成的。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 26
char str[50017][117];
typedef struct Trie
{
int v;//依据须要变化
Trie *next[MAXN];
//next是表示每层有多少种类的数,假设仅仅是小写字母,则26就可以,
//若改为大写和小写字母,则是52,若再加上数字,则是62了
}Trie;
Trie *root; void createTrie(char *str)
{
int len = strlen(str);
Trie *p = root, *q;
for(int i = 0; i < len; i++)
{
int id = str[i]-'a';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
q->v = 1;//初始v==1
for(int j = 0; j < MAXN; j++)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
// p->next[id]->v++;
p = p->next[id];
}
}
p->v = -1;//若为结尾,则将v改成-1表示
} int findTrie(char *str)
{
int len = strlen(str);
Trie *p = root;
for(int i = 0; i < len; i++)
{
int id = str[i]-'a';
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return 0;
// if(p->v == -1) //字符集中已有串是此串的前缀
// return -1;
}
//return -1; //此串是字符集中某串的前缀
if(p->v == -1)//说明是全然匹配
return -1;
else
return 0;
}
int dealTrie(Trie* T)
{
//动态字典树,有时会超内存,这是就要记得释放空间了
if(T==NULL)
return 0;
for(int i = 0; i < MAXN; i++)
{
if(T->next[i]!=NULL)
dealTrie(T->next[i]);
}
free(T);
return 0;
}
int main()
{
root = (Trie *)malloc(sizeof(Trie));
for(int i = 0; i < MAXN; i++)
root->next[i] = NULL;
int cont = 0;
while(scanf("%s",str[cont])!=EOF)
{
createTrie(str[cont]);
cont++;
}
char a[117], b[117];
for(int i = 0; i < cont; i++)
{
int len = strlen(str[i]);
for(int j = 1; j < len; j++)
{
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
strncpy(a,str[i],j);
strncpy(b,str[i]+j,len-j);
if(findTrie(a)==-1 && findTrie(b)==-1)
{
printf("%s\n",str[i]);
break;
}
}
}
return 0;
}

最新文章

  1. Transport Block Size, Throughput and Code rate-----http://www.simpletechpost.com/2012/12/transport-block-size-code-rate-protocol.html
  2. Sqli-LABS通关笔录-14
  3. button 浏览器兼容问题
  4. js jquery 判断IE有效方法
  5. Python模块整理(三):子进程模块subprocess
  6. Hard 不用+号实现两个数之和 @CareerCup
  7. 元素NULL判断
  8. 最小点集覆盖=最大匹配&lt;二分图&gt;/证明
  9. JavaScript数字例子,二分法,冒泡排序
  10. [Code] 递归函数在函数式 Java 中的实现
  11. 如何把Excel中的E+数值批量修改为文本格式?
  12. 获取ADO连接字符串
  13. 利用 DynamicLinq 实现简单的动态表达式构建查询
  14. 记录: Android测试网速实现
  15. C# wkhtmltopdf 将html转pdf
  16. BZOJ4710: [Jsoi2011]分特产【组合数学+容斥】
  17. Redis学习---Redis操作之String
  18. Java测试各种数据库连接(用Connection类)
  19. Spark源代码阅读笔记之MetadataCleaner
  20. HDU 4863 Centroid of a Tree

热门文章

  1. bzoj 4804 欧拉心算 欧拉函数,莫比乌斯
  2. codeforces 900D 数论+组合+容斥原理
  3. 洛谷P1120 小木棍(sticks数据加强版)
  4. 【CF711C】Coloring Trees(DP)
  5. 关于记忆力:遵从一些原则,自省增加经验,there is a way out of almost everything
  6. poj 1066 Treasure Hunt 线段相交
  7. 解决 unresolved external symbol 无法解析 _send@16(转)
  8. linux信号-------初涉
  9. Day 19 函数之闭包、装饰器
  10. 这是一份很有诚意的2017 Google I/O大会的汇总 &amp; 解析