hdu 1247 Hat’s Words(从给的单词中找hat's word 并按字典序输出)

1.在使用mp[key]的时候它会去找键值为key的项,如果没有,他会自动添加一个key的项,再把value赋值为相应的初始值(value是int的话赋值为0,string的话赋值为空)。所以如果是插入的话可以用insert,如果是查找的话可以使用find,这样可以节省开销。查找的时间复杂度为O(logn)

2.

代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;

string word[50005];
map<string,int> mp;//用平衡二叉树实现的,按key从小到大排

int main()
{
    int i=0;
    //printf("%d\n",k.max_size());用来看容器的容量
    while(cin>>word[i++])//用ctrl+z然后enter结束循环
    {
        mp[word[i-1]]=1;//mp[]中的[]已重载,注意是i-1啊啊!!!!!!
    }
    map<string,int>::iterator it;
    for(it=mp.begin(); it!=mp.end(); it++)
    {
        string w=it->first;
        for(i=1; i<w.length()-1; i++)
        {
            string w1(w,0,i);//复制[0,i)
            string w2(w,i);//从w的i位置开始复制
            if(mp.find(w1)!=mp.end()&&mp.find(w2)!=mp.end())
            {
                cout<<w<<endl;
                break;
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1247 Hat’s Words(从给的单词中找hat's word 并按字典序输出)

时间: 2024-07-30 11:33:23

hdu 1247 Hat’s Words(从给的单词中找hat's word 并按字典序输出)的相关文章

hdu 1247 Hat’s Words(map)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8527    Accepted Submission(s): 3069 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

HDU 1247 Hat’s Words (字符串匹配,暴力)

题意:给出一堆单词,如果有一个单词可以分成左右串两个单词,并且在所给的一堆单词中存在,就是hat词,统计所有这样的词,并按字典序输出. 思路:注意定义,一个hat词可以被两部分已经存在的词组成,那么也可以是由两个相同的词组成,比如{abcabc,abc} 这样的abcabc也是满足条件的.解法,将所有的单词塞到一个哈希set中,遍历其中每个单词,对每个单词进行任意可能的拆解成两部分,在该set中找,如果两个部分都找到了,那么就成功.塞到另一个set中,最后将set中全部输出.

HDU - 1247 - Hat’s Words (字典树!!)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8579    Accepted Submission(s): 3090 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

HDU 1247 Hat&#39;s Words (字典树)

[题目链接]click here~~ [题目大意]A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. ,找出由两个子字符串组成的字符串. [解题思路]字典树 #include <bits/stdc++.h> using namespace std; const int N=5*1e4+100; const int MOD=

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

hdu 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

HDU 1247 Hat’s Words Trie题解

使用Trie的insert函数,主要是要灵活修改search函数,使得其可以快速搜索hat word. 思路: 1 先搜索一个word的前缀有没有在Trie树中找到,如果找到,保存这个Node,方便下面继续搜索, 就搜索余下的能不能是Trie树上的一个单词,如果找到,那么就是hat word了. 2 如果没找到,那么就沿着刚刚搜索到的前缀单词的节点继续往下搜,这样就可以加速程序,不用每次重复搜索. 3 如果沿着Trie树已经走到word的叶子节点了,那么就结束这个word的搜索了. 实现好这些思

HDU 1247 Hat&#39;s words(字典树Trie)

解题思路: 判断给出的单词是否恰好由另外两个单词组成,用栈保存每个子字符串的节点,从这个节点出发判断剩下的字符串是否在字典树中即可. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <sta

hdu 1247 Hat’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a