杭电 HDU 1247 ACMHat’s Words(trie树 或着STL)

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9620    Accepted Submission(s): 3438

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

Author

戴帽子的

昨天晚上用STL 1a渺杀 ,今天改用trie树 wa一上午 也是醉了!指针真的好烦人!

STL:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;

int main()
{
    string str;
    map<string,int >cnt;
    set<string>dict;
    vector<string>buf;
    while(cin>>str)
    {
        cnt[str]=1;
        buf.push_back(str);
    }
    for(int i=0; i<buf.size(); i++)
    {
        string str2;
        for(int j=0; j<buf[i].size()-1; j++)
        {
            str2+=buf[i][j];
            if(cnt.count(str2)&&cnt.count(buf[i].substr(j+1,buf[i].size()-j-1)))
            {
                dict.insert(buf[i]);
                break;
            }
        }
    }

    for(set<string>::iterator it=dict.begin(); it!=dict.end(); it++)
        cout<<*it<<endl;
    return 0;
}

TRIE树:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
char cnt[50031][153];

struct Node
{
    struct Node *next[26];
    bool isword;
    Node()
    {
        memset(next,NULL,sizeof(next));
        isword=0;
    }
}*root;

void insertWord(Node * node ,char *co)
{
    int id;
    node = root ;
    while(*co)
    {
        id=*co-'a';
        if(node->next[id]==NULL)
            node->next[id]=new Node;
        node = node->next[id];
        co++;
    }
    node->isword=1;
}

bool searchWord(Node *node ,char *co)
{

    int flag=0;
    node = root;
    while(*co)
    {
        int id=*co-'a';
        if(node->next[id])
        {
            node=node->next[id];
            if(node->isword)
            {
                flag=1;
                Node *p=root;
                char *pt=co;
                pt++;
                while(*pt)
                {

                    int id=*pt-'a';
                    if(p->next[id])
                        p=p->next[id];
                    else
                    {
                        flag=0;
                        break;
                    }
                    pt++;
                }
                if(flag)
                {
                    if(p->isword)
                        return 1;
                }

            }

        }
        co++;
    }
    return 0;
}

int main()
{
    int t=0;
    root=new Node;
    while(~scanf("%s",cnt[t++]))
    {
        insertWord(root,cnt[t-1]);
    }
    for(int i=0; i<t; i++)
    {
        if(searchWord(root,cnt[i]))
        {
            printf("%s\n",cnt[i]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 20:42:45

杭电 HDU 1247 ACMHat’s Words(trie树 或着STL)的相关文章

hdu 1247 Hat’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a

杭电 HDU ACM 2795 Billboard(线段树伪装版)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14144    Accepted Submission(s): 6058 Problem Description At the entrance to the university, there is a huge rectangular billboard of

杭电 HDU ACM 1225 Atlantis (线段树 扫描线 离散化 最基本)

acm第一发扫描线问题,其实算法原理很好理解 ,但是具体实现起来还是遇到诸多问题,我尝试参考了网上两份对于解决 线段树表示区间问题的方法, 第一种是每个结点的真实值,比如对于更新离散化后的1 ~ 4区间,我们在线段树中更新的是1 ~ 3,这样单个结点也可以表示一个最小单位区间. 第二种那就是在建树的时候改变通常策略,1 ~ 10 为总区间,两个孩子为1 ~ 5 ,5 ~ 10. 核心难点:当我们每次找到需要更新的区间,首先应该更新cover值,然后判断此时是被覆盖了,还是解除覆盖了,如果刚好被覆

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

HDU 1247 Hat’s Words Trie题解

使用Trie的insert函数,主要是要灵活修改search函数,使得其可以快速搜索hat word. 思路: 1 先搜索一个word的前缀有没有在Trie树中找到,如果找到,保存这个Node,方便下面继续搜索, 就搜索余下的能不能是Trie树上的一个单词,如果找到,那么就是hat word了. 2 如果没找到,那么就沿着刚刚搜索到的前缀单词的节点继续往下搜,这样就可以加速程序,不用每次重复搜索. 3 如果沿着Trie树已经走到word的叶子节点了,那么就结束这个word的搜索了. 实现好这些思

杭电 HDU 1098 Ignatius&#39;s puzzle

Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7068    Accepted Submission(s): 4883 Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

杭电hdu 4861 Couple doubi

杭电 2014多校联训第一场   1001   Couple doubi   逗比夫妇 这标题我就不多说什么了. 题意:有K个球在桌上,每个球都有价值,第i个球的价值是1^i+2^i+...+(p-1)^i (mod p).其中p是一个素数,之后逗比男先选球,最后所有球总分高的获胜.如果逗比男获胜,那么输出“YES”否则输出“NO”.(逗比男和逗比女都采取最有策略). 当然这也p是奇素数的一个重要公式.曾有题是这个公式求和.当然如果你知道就很简单了.如果不知道,就打表找规律吧. 根据这一重要的公