STL MAP使用注意事项
Hat’s Words
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使用注意事项的更多相关文章
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- [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 ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
随机推荐
- js原型链,作用域,闭包讲解
当面试的时候遇到问原型链,闭包,还有作用域,直接 拿张纸和笔把原型链画出来,闭包跟作用域直接用笔写几道题出来加深理解(因为我们是理科生,图形和题目以及控制台输出结果才是最直观的方法) 问:什么是原型链 ...
- Scrapy框架中的Pipeline组件
简介 在下图中可以看到items.py与pipeline.py,其中items是用来定义抓取内容的实体:pipeline则是用来处理抓取的item的管道 Item管道的主要责任是负责处理有蜘蛛从网页中 ...
- Bonetrousle HackerRank 数学 + 思维题
https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求 ...
- Docker 镜像制作 CentOS+JDK+Tomcat
[root@localhost createImages]# ls apache-tomcat-.tar.gz server-jre-8u121-linux-x64.tar.gz [root@loca ...
- 系统启动与Linux的初始化_1
boot 的起源: the term bootstrap refers to a person who tries to stand up by pulling her own boots. (Und ...
- SQLServer 索引的使用情况
—在优化查询SQL语句,查看索引使用情况SQL语句: select db_name(database_id) as N'数据库名称', object_name(a.object_id) as N'表名 ...
- Device
#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelega ...
- 【mysql】mysql 配置
安装完mysql后, 要及得配置一下 /etc/mysql/my.cnf 配置字符编码为utf8 [client] default-character-set = utf8 [mysqld] defa ...
- ngnix反向代理
https://blog.csdn.net/sherry_chan/article/details/79055211
- CPU保护模式DPL、CPL简易理解
现代INTEL CPU都有保护模式,实模式这两种CPU运行模式.当CPU加电,CPU初始化时就运行在是模式下,然后现代操作系统会从实模式跳转到保护模式! 为什么需要保护模式? 在最开始编程的汇编时代, ...