HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

题目

用map写超便捷

也可以用字典树来写

我以前是用map的:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#include <iostream>
#include<map>
using namespace std;
string word[50010];
int main()
{
    int i=0,n,len,j;
    map<string,int>m;
//    map<string,int>::iterator q;
    while(cin>>word[i])
    {
        m[word[i++]]=1;//这里是标记该单词在map里的
    }
    n=i;
    for(i=0;i<n;i++)
    {
        len=word[i].length();
        for(j=0;j<len;j++)
        {
            string s1(word[i],0,j);//C++中string的操作:复制函数:word[i]的 0~j 位复制给s1
            string s2(word[i],j);//同上:复制函数:j~末位(前边必要加string 定义?)
            if(m[s1]==1&&m[s2]==1)
            {
                cout<<word[i]<<endl;
                break;
            }
        }
    }
    return 0;
}

HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

时间: 2024-08-05 11:18:22

HDU 1247 Hat’s Words(map,STL,字符处理,string运用)的相关文章

HDU 1247 Hat&#39;s Words (map+string)

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

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&#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(从给的单词中找hat&#39;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; stri

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)

HDU 1247 Hat's words(Trie) ACM 题目地址: HDU 1247 Hat's words 题意: 给些单词,问每个单词是否能用另外两个单词拼出. 分析: 直接保存到trie里面,然后暴力切割查询即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1247.cpp * Create Date: 2014-09-24 11:04:11 * Descripton: */ #include <cstdio