STL MAP使用注意事项

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




 

posted @ 2017-08-08 23:11  佰大于  阅读(357)  评论(0编辑  收藏  举报