• 杭电 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
    ",cnt[i]);
            }
        }
        return 0;
    }
    


  • 相关阅读:
    BZOJ-2743: [HEOI2012]采花(树状数组 or TLE莫队)
    BZOJ-1122: [POI2008]账本BBB (单调栈神题)
    2017年10月18日23:54:18
    [校内自测 NOIP模拟题] chenzeyu97要请客(单调栈)
    BZOJ-1057: [ZJOI2007]棋盘制作(单调栈)
    [校内自测] 奶牛编号 (递推+智商)
    [校内自测] Incr (LIS+智商)
    BZOJ1486 [HNOI2009]最小圈
    BZOJ2400 Spoj 839 Optimal Marks
    BZOJ2595 [Wc2008]游览计划
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7009478.html
Copyright © 2020-2023  润新知