HDU 1247 Hat’s Words

来源:互联网 发布:plsql导入表数据库 编辑:程序博客网 时间:2024/05/16 02:14

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

aahathathatwordhzieeword

Sample Output

ahat

hatword

直接用map就好了。

#include<map>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 50005;map<string, int> M;int tot;string s[maxn];int main(){    for (int i = tot = 0; cin >> s[i]; tot++, i++) M[s[i]] = 1;    for (int i = 0; i < tot; i++)        for (int j = 0; s[i][j]; j++)            if (M[s[i].substr(0, j + 1)])             if (M[s[i].substr(j + 1, s[i].size())])            {                cout << s[i] << endl;                 break;            }}


0 0
原创粉丝点击