HDU 1247 Hat’s Words

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

直接用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;
            }
}


你可能感兴趣的:(HDU)