Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11314    Accepted Submission(s): 4041

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
题解:判断单词是否是由两个单词合并得到,加了个val判断是否为一个完整的单词;本来还以为是只与hat结合呐,神经的只减去hat,谁知道,自己想错了。。。是两个单词的结合;
代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int INF=0x3f3f3f3f;
 9 const double PI=acos(-1.0);
10 const int MAXN=50010;
11 int ch[MAXN][30],word[MAXN],val[MAXN];
12 char dt[50010][110];
13 int sz;
14 void initial(){
15     sz=1;
16     mem(ch[0],0);mem(word,0);mem(val,0);
17 }
18 void join(char *s){
19     int len=strlen(s),k=0,j;
20     for(int i=0;i<len;i++){
21         j=s[i]-'a';
22         if(!ch[k][j]){
23             mem(ch[sz],0);
24         //    val[sz]=0;
25             ch[k][j]=sz++;
26         }
27         k=ch[k][j];
28         word[k]++;
29     }
30     val[k]=1;
31 }
32 bool find(char *s){
33     int len=strlen(s),k=0,j;
34     for(int i=0;i<len;i++){
35         j=s[i]-'a';
36         k=ch[k][j];
37         if(!word[k])return false;
38     }
39     if(val[k])return true;
40     else return false;
41 }
42 int main(){
43     initial();
44     int t=0;
45     char s[110],l[110],r[110];
46     while(~scanf("%s",dt[t]))join(dt[t++]);
47     for(int i=0;i<t;i++){
48         int len=strlen(dt[i]);
49         if(len<=1)continue;
50         for(int j=1;j<len;j++){
51             strcpy(r,dt[i]+j);
52             strcpy(l,dt[i]);
53             l[j]='\0';
54             if(find(l)&&find(r)){
55                 puts(dt[i]);break;
56             }
57         }
58     }
59     return 0;
60 }

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace  std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1000010;//要开的足够大
int ch[MAXN][30];
int word[MAXN];
char str[50010][110];
int val[MAXN];
int sz;
int N;
char l[110],r[110];
void insert(char *s){int k=0,j;for(int i=0;s[i];i++){j=s[i]-'a';if(!ch[k][j]){mem(ch[sz],0);ch[k][j]=sz++;}k=ch[k][j];word[k]++;}val[k]=1;
}
bool find(char *s){int k=0,j;for(int i=0;s[i];i++){j=s[i]-'a';k=ch[k][j];if(!word[k])return false;}if(val[k]!=1)return false;return true;
}int main(){int tp=0;sz=1;mem(ch[0],0);mem(val,0);mem(word,0);while(~scanf("%s",str[tp]))insert(str[tp++]);
//  printf("%d\n",tp);for(int i=0;i<tp;i++){for(int j=0;str[i][j];j++){strcpy(l,str[i]);l[j]='\0';strcpy(r,str[i]+j);//      printf("**%s %s\n",l,r);if(find(l)&&find(r)){puts(str[i]);break;}}}return 0;
}

  

转载于:https://www.cnblogs.com/handsomecui/p/4918496.html

Hat’s Words(字典树)相关推荐

  1. HDU 1247 Hat’s Words 字典树(Trie树)

    HDU 1247 Hat's Words 字典树(Trie树) 字典树的建立是应该都是一样的 下面是我的做法: 建立完后, 对每一个单词都进行find_string()判断是否符合, 分别对其分成两半 ...

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

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

  3. HDU1247 字典树 Hat’s Words(Tire Tree)

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

  4. hdu 1251 统计难题(字典树)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 分析: 关于字典树的题目似乎都有一个普通适用性的模板,有时只是稍加改动来满足临时的要求,我的一 ...

  5. 字符串处理——字典树

    [概述] 字典树,又称为单词查找树,Tire 树,是一种树形结构,它是哈希树的变种. 字典树与字典很相似,当要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典 ...

  6. 字典树,01字典树,可持续化01字典树(总结+例题)

    目录 字典树 01字典树 字典树例题: power oj 2390: 查单词 HDU 1671 Phone List HDU 1004Let the Balloon Rise HDU 1075 Wha ...

  7. hdu5296 01字典树

    根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...

  8. BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...

  9. 2014百度之星 Xor Sum(字典树+贪心)

    题目在HDU_OJ Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometh ...

  10. HDU6964 I love counting (字典树+莫队)

    题意: 给定一个长度为 nnn 的序列c,qc,qc,q 次询问,每次给出l,r,a,bl,r,a,bl,r,a,b求在[l,r][l,r][l,r]中有多少种不同的值 kkk 满足 k⊕a≤b​.k ...

最新文章

  1. UIPickerView隐藏透明条
  2. Intel Hex格式说明
  3. windows下编译支持https的curl
  4. 大二上期计算机试题答案,2019年对口升学班上期期末计算机网络技术试卷及答案知识讲解.doc...
  5. 5款最适合新手的包管理器
  6. [Flags]标识的Enum不能使用Html.GetEnumSelectList方法
  7. SQL Server 视图设计器
  8. Nginx配置文件conf详解
  9. 就算神游 之五:东京迪斯尼乐园 1
  10. 命令行 移动整个文件夹 -baijiahao_Windows高手的高效办公利器——Windows命令行简介...
  11. [渝粤教育] 新疆财经大学 金融工程 参考 资料
  12. JSP教程第2讲笔记
  13. Xshell5 破解
  14. Cache的Insert 和Add 方法引发的血案
  15. 2019天梯赛+第一次面试总结
  16. 思迈特软件Smartbi!这才是你该选用的企业bi报表工具!
  17. 【python教程入门学习】第一个Pygame程序
  18. visio 2010 技巧
  19. Studio3t 过期激活办法/以及重新设置使用日期的脚本不可用解决办法/Studio 3T无限激活原创
  20. java8的时期和时间

热门文章

  1. 正确“假期休息模式”
  2. hadoop问题小结
  3. PyTorch的torch.cat
  4. python究竟要不要使用多线程
  5. LeetCode简单题之数组中两元素的最大乘积
  6. 在cuDNN中简化Tensor Ops
  7. 自动驾驶仿真分析,提高研发效率
  8. 解读模拟摇杆原理及实验
  9. PyTorch神经网络集成技术
  10. 激光雷达和V2X技术