HDU 1247 Hat’s Words

来源:互联网 发布:2017最火的中文编程 编辑:程序博客网 时间:2024/06/06 05:45
Hat’s Words

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
大致题意:给出一堆单词,找出其中可以由另外两个单词组成的单词并输出
分析:(1)Trie的应用:先将所有的单词保存在树中,然后将每个单词从各个位置分成两段,判断这两部分是否在树中。如果都在树中,则满足条件。(2)用STL:将每个单词放入map容器中,并进行标记,再用动态数组存放这些单词,将单词分为两部分,判断这两部分在map容器中是否被标记,如果都被标记,则此单词满足条件。
(一)Trie
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;char s[50000][50];struct Trie     //字典树{    int Isword; //判断该节点是否为单词    Trie *next[26];    Trie()//构造函数,每创建一颗字典树时自动调用    {        Isword = 0;        for(int i = 0;i <26;i++)            next[i] = NULL;    }}*Root;void Insert(char ch[])//插入一个单词{    Trie *p = Root;    for(int i = 0;ch[i];i++)    {        int k = ch[i] - 'a';        if((*p).next[k] == NULL)        {            Trie *temp = new Trie;            (*p).next[k] = temp;        }        p = (*p).next[k];    }    (*p).Isword = 1;}bool search(char ch[])//查找单词是否在树中{    Trie *p = Root;    for(int i = 0;ch[i];i++)    {        int k = ch[i] - 'a';        if((*p).next[k] == NULL || p == NULL)            return 0;        p = (*p).next[k];    }    return (*p).Isword;}void Delete(Trie *root)//递归释放内存空间{    for(int i = 0;i < 26;i++)    {        if((*root).next[i] != NULL)            Delete((*root).next[i]);    }    delete(root);}int main(){    Root = new Trie;    int t = 0;    while(~scanf("%s",s[t]))    {        Insert(s[t]);        t++;    }    for(int i = 0;i < t;i++)    {        int len = strlen(s[i]);        for(int j = 1;j < len;j++)        {            char t1[50] = {'\0'},t2[50] = {'\0'};            strncpy(t1,s[i],j);   //将单词截断为两部分            strncpy(t2,s[i] + j,len - j);            if(search(t1) && search(t2))            {                printf("%s\n",s[i]);                break;            }        }    }    Delete(Root);    return 0;}

(二)STL
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<map>#include<set>#include<string>#include<vector>using namespace std;int main(){    string st;    vector <string> v;    set<string>s;    map<string,int> m;    while(cin >> st)    {        m[st] = 1;        v.push_back(st);    }    for(int i = 0;i < v.size();i++)    {        string ss;        for(int j = 0;j < v[i].size() - 1;j++)        {            ss += v[i][j];            if(m[ss] && m[v[i].substr(j + 1,v[i].size() - j - 1)])            {                s.insert(v[i]);                break;            }        }    }    set<string>::iterator it;    for(it = s.begin();it != s.end();it++)        cout << *it << endl;    return 0;}



0 0