HDU - Hat’s Words(STL)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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

Problem solving report:

Description: “帽子单词”是指,若字典中的某个词,它是由其他任意两个单词连接起来组成的,则称它为“帽子单词”。
现在以字典序给出字典中的所有单词(不超过5e4个),让你求出全部“帽子单词”。
Problem solving: 先把字典存到set里面,然后对于每个单词,暴力地分成两个子串查找即可。

Accepted Code:

#include <bits/stdc++.h>
using namespace std;
int main() {
    char str[100];
    set <string> spt;
    while (~scanf("%s", str))
       spt.insert(str);
    set <string>::iterator it;
    for (it = spt.begin(); it != spt.end(); it++) {
        string a = *it;
        for (int j = 1; j < a.size(); j++) {
            string s1(a, 0, j);
            string s2(a, j);
            if (spt.find(s1) != spt.end() && spt.find(s2) != spt.end()) {
                printf("%s\n",a.c_str());
                break;
            }
        }
    }
    return 0;
}
全部评论

相关推荐

头像 会员标识
12-16 14:43
浙江大学 Java
投递牛客等公司10个岗位
点赞 评论 收藏
分享
11-25 09:41
已编辑
Java
程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务