• 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=='')
            return;
        int i;
        Trie *p=root;
        while(*s!='')
        {
            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!='')
        {
            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!='')
        {
            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
    ",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;
    }
    
    
    



  • 相关阅读:
    javascript学习笔记
    c++输入文件流ifstream用法详解
    如何退出while(cin >> s)
    【赵渝强老师】利用Python完成数据分布特征的分析
    PG 的 AWR pg_profile
    java构建树形结构
    docker安装mongodb 数据备份和还原
    通过经纬度计算不同模型图瓦片坐标
    linux常用命令
    docker 常用命令
  • 原文地址:https://www.cnblogs.com/TianyuSu/p/9382808.html
Copyright © 2020-2023  润新知