HDU 1247 Hat'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 exactly two other words in the dictionary. You are to find all the hat’s words in a dictionary.

Input

Standard 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.

Output

Your 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<cstdio>
#include<string>
#include<map>
using namespace std;

map<string,int> M;
string str[50005];

int main()
{
    //freopen("in.txt", "r", stdin);
    int k = -1;
    while(cin>>str[++k])
    M[str[k]] = 1;
    for(int i=0; i<=k; i++)
    {
        int len= str[i].size()-1;
        for(int j=1; j<len; j++)
        {
            string s1(str[i], 0, j);
            string s2(str[i], j);
            if(M[s1]==1&&M[s2]==1)
            {
                cout<<str[i]<<endl;
                break;
            }
        }
    }
    return 0;
}

本题技巧: string  s1(str[i], 0, j);把str[i]中的0~j的字符赋给s1. string s2(str[i], j)把str[i]中的j~末尾的字符赋给s2.

HDU 1247 Hat's Words (map+string)

时间: 2024-08-07 08:36:40

HDU 1247 Hat's Words (map+string)的相关文章

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(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() { i

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(字典树,经典题)

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 字典树

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

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