Hat’s Words

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. 

InputStandard 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. 
OutputYour 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 <iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define MAX 26 using namespace std; typedef struct TrieNode //Trie结点声明
{
bool isStr; //标记该结点处是否构成单词
struct TrieNode *next[MAX]; //儿子分支
}Trie; void insert(Trie *root,const char *s) //将单词s插入到字典树中
{
if(root==NULL||*s=='\0')
return;
int i;
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL) //如果不存在,则建立结点
{
Trie *temp=(Trie *)malloc(sizeof(Trie));
for(i=0;i<MAX;i++)
{
temp->next[i]=NULL;
}
temp->isStr=false;
p->next[*s-'a']=temp;
p=p->next[*s-'a'];
}
else
{
p=p->next[*s-'a'];
}
s++;
}
p->isStr=true; //单词结束的地方标记此处可以构成一个单词
} int search(Trie *root,const char *s) //查找某个单词是否已经存在
{
Trie *p=root;
while(p!=NULL&&*s!='\0')
{
p=p->next[*s-'a'];
s++;
}
return (p!=NULL&&p->isStr==true); //在单词结束处的标记为true时,单词才存在
}
int search1(Trie *root,const char *s) //查找某个单词是否已经存在
{
Trie *p=root;
while(p!=NULL&&*s!='\0')
{
if(p->isStr)//说明该单词的某个前缀是一个单词
if(search(root,s))//说明该单词去掉前缀后仍然是一个单词
return 1;
p=p->next[*s-'a'];
s++;
}
return 0; //
}
void del(Trie *root) //释放整个字典树占的堆区空间
{
int i;
for(i=0;i<MAX;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
free(root);
}
char s[50005][20];
int main()
{
Trie *root = (Trie *)malloc(sizeof (Trie));
for(int i=0;i<MAX;i++)
root->next[i] = NULL;
root->isStr = false;
int cnt = 0;
while(~scanf("%s",s[cnt]))
{
insert(root,s[cnt++]);
}
for(int i=0;i<cnt;i++)
{
if(search1(root,s[i]))
printf("%s\n",s[i]);
}
return 0; }

也可以用 STL 做,但是在使用 set 的find方法查找某个单词的时候会出问题,不停地WA 。只能改成MAP,但是MAP也是坑。用的时候要注意

#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, int>m;
int main() {
string str;
m.clear();
while (cin >> str) m[str] = 1;
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
if (it->second)//这句话,必须加上。否则就WA,但是set也可以实现但是不能判断,find函数有点问题。因此能用set就换成MAP吧。然而不造为啥。。
for (int i = 1; i < (it->first).size(); i++)
if (m[(it->first).substr(0, i)] == 1 && m[(it->first).substr(i)] == 1) {
cout << it->first << endl;
break;
}
return 0;
}

最新文章

  1. 常用js归纳
  2. HTTP 302 404 500 状态消息
  3. Gson解析json数据
  4. spring mvc 框架核心文档
  5. bash 脚本
  6. easyui 中的treegrid添加checkbox
  7. HDU 4849-Wow! Such City!(最短路)
  8. 宏定义 button 方法 --备
  9. grub 的安装与使用
  10. 运行ORB-SLAM笔记_使用篇(二)
  11. CURL C++网络延时或者最低网速下载设置
  12. 归并排序(Java)
  13. easygui的导入方式
  14. mysql explain执行详解
  15. Flask入门的第一个项目进阶版
  16. eclipse2019-03设置代码编辑区背景为图片
  17. Springzz中使用监听器,用于容器一启动就加载准备数据(application范围内的数据,用于减轻服务器压力,不用每次都去查数据)
  18. 《DSP using MATLAB》Problem 5.21
  19. redis on windows
  20. CF529B 【Group Photo 2 (online mirror version)】

热门文章

  1. Error: npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
  2. Anniversary party (树形DP)
  3. 瓷砖覆盖(状压DP)
  4. (六)Redis主从自动恢复-sentinel
  5. Java排序算法(三)
  6. mysql存储方式MyISAM 和 InnoDB的区别
  7. 禁止Asp.Net WebService 的Test页面功能
  8. android中AudioRecord使用
  9. java网络访问指定出口ip
  10. Don&#39;t let anyone tell you different.