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. java 集合解析

    Set集合,放的元素不能重复,请问它的判断重不重复是怎么实现的? 比如说:ArrayList 和 Vector 是用数组的方式存储的Set里的 hashSet 和TreeSet是用什么方式存储的?怎么 ...

  2. jQuery Plugin Poshy Tip 使用 统一提示信息

    项目到了后期,发现前端的提示信息不统一,解决思路如下: 1.回顾系统中tip出现的场景:表单验证提示信息.数据列表中随填随显 2.确定问题域:多条提示信息层叠.信息显示风格不统一 3.结论:找出一款合 ...

  3. 使用C#连接 MyCat 链接串

    所属专栏: mycat的安装部署以及监控和运维    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014180504/article/detai ...

  4. python入门之time模块和datetime模块

    time模块 时间三种表示:时间戳(秒单位),struct_time(元组,可以分开调用),指定格式(格式化) time.sleep() 等待5秒钟 time.time() 返回时间戳 time.ct ...

  5. t-ora issue can't login mysql

    https://github.com/tora-tool/tora/issues/99 ### TOra is an open-source multi-platform database manag ...

  6. Murano中的角色

    Application Publisher - An individual or company that publishes an application to an application cat ...

  7. php 转码函数 你还在用iconv吗?-- 解决sqlserver插入中文失败问题

    文章来源 :http://www.veryhuo.com/a/view/41348.html 这次给客户同步sqlserver数据,临时搭的 PHP Query Analyzer 插入某些中文一直有些 ...

  8. spring-boot 配置jsp

    sring-boot 集成  jsp spring-boot默认使用的页面展示并不是jsp,若想要在项目中使用jsp还需要配置一番. 虽然spring-boot中也默认配置了InternalResou ...

  9. jsp连接sqlite、Sqlite相对路径绝对路径问题(转)

    转自  http://blog.csdn.net/sxy12138/article/details/52304884 假如在java中, # 数据库连接jdbc.jdbc-url=jdbc:sqlit ...

  10. CentOS6.x升级MySQL 5.1到5.6

    有一些虚拟机.云主机提供商仍然使用的是老版本的安装套件.预装的应用软件版本很低.有些IDC使用的云服务器,其中MySQL预装版本为老版本5.1.x.而较新的MySQL 5.6版本在性能.功能.安全性等 ...