zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    



  • 相关阅读:
    慎用const_cast
    python中string的操作函数
    C++ Const总结
    python dict sorted 排序
    "没有找到MSVCP80D.dll,因此这个应用程序未能启动。重新安装应用程序...
    提高你开发效率的十五个 Visual Studio 使用技巧
    一些 python 插件 编译安装的注意事项
    html 制作表格 合并 样式
    将一个表的数据更新到另一个表中
    屏蔽web页面的右键,但不屏蔽输入框中的右键
  • 原文地址:https://www.cnblogs.com/TianyuSu/p/9382808.html
Copyright © 2011-2022 走看看