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;
}

STL MAP使用注意事项的更多相关文章

  1. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  2. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  3. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  4. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  5. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  6. STL MAP 反序迭代

    ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...

  7. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  8. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

  9. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

随机推荐

  1. df 参数说明

    df -h 由 df 命令显示出的各列信息的含义分别是: Filesystem:表示该文件系统位于哪个分区,因此该列显示的是设备名称: 1K-blocks:此列表示文件系统的总大小,默认以 KB 为单 ...

  2. [Leetcode]006. ZigZag Conversion

    public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...

  3. eclipse ctrl+h

    之前我的eclipse使用ctrl + h 出现的总是Plug-in Search ,总是需要点那个小三角才能找到File Search ,深感使用不便, 修改方法:Window->Genera ...

  4. 几种复杂度的斐波那契数列的Java实现

    一:斐波那契数列问题的起源 13世纪初期,意大利数论家Leonardo Fibonacci在他的著作Liber Abaci中提出了兔子的繁殖问题: 如果一开始有一对刚出生的兔子,兔子的长大需要一个月, ...

  5. 借鉴redux,实现一个react状态管理方案

    react状态管理方案有很多,其中最简单的最常用的是redux. redux实现 redux做状态管理,是利用reducer和action实现的state的更新. 如果想要用redux,需要几个步骤 ...

  6. java多线程,如何防止脏读数据

    多线程容易“非线程安全”的情况,是由于用了全局变量,而又没有很好的控制起情况.所以无论做什么程序,谨慎使用全局变量 "非线程安全"其实会在多个线程对同一个对象中的实例变量进行并发访 ...

  7. CXF 发布rest服务

    1.1      什么是rest服务 REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位. Rest要求对资源定位更加准 ...

  8. JavaFX--第2天-窗口基本的类

    1 内部匿名类和Lambda表达式 2 Switching Scene 3 信息提示框 (Alert Boxes) 前情回顾: 前面的学习内容:关于JavaFX的基本概念,以及窗口所使用的类的一个介绍 ...

  9. 关于小程序后台post不到数据的问题

    小程序post请求获取不到数据问题 把headers的参数“Content-Type”的值改为application/x-www-form-urlencoded: Request Body Type ...

  10. 学习笔记:MDN的JavaScript

    JavaScript 第一步 什么是JavaScript? 每次当你浏览网页时不只是显示静态信息—— 显示即时更新的内容, 或者交互式的地图,或 2D/3D 图形动画,又或者自动播放视频等,你可以确信 ...