zoukankan      html  css  js  c++  java
  • Hat’s Words HDU

    Hat’s Words HDU - 1247

    题目链接:https://vjudge.net/problem/HDU-1247

    题目:

    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.

    InputStandard 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.
    OutputYour 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
    题意:就是给你一个单词列表,然后单词可以从中间拆成任意两部分,只要这两部分都是单词列表里的就把这个单词输出,否则不输出
    思路:利用字典树将所有单词插入字典树中,单词个数均赋予1,然后后面处理的时候再利用strncpy函数将单词分开,分别存入s1,s2中,注意末尾要赋予“”
    才可使用,然后就利用字典树开始找,当find(s1)+find(s2)为2时,说明都找到了,此时将字符串输出即可;

    // 
    // Created by HJYL on 2019/8/21.
    //
    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    #include <queue>
    #include <stack>
    #include <set>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    struct trie{
        int sum;
        trie* next[26];
        trie(){
            for(int i=0;i<26;i++)
                next[i]=NULL;
            sum=0;
        }
    }root;
    void insert(char* s)
    {
        trie* p=&root;
        for(int i=0;s[i];i++)
        {
            if(p->next[s[i]-'a']==NULL)
                p->next[s[i]-'a']=new trie;
            //else
                p=p->next[s[i]-'a'];
        }
        p->sum=1;//单词存在就是1
    }
    int find(char* s)
    {
        trie* p=&root;
        for(int i=0;s[i];i++)
        {
            if(p->next[s[i]-'a']==NULL)
                return 0;
            else
                p=p->next[s[i]-'a'];
        }
        return p->sum;
    }
    int main()
    {
        //freopen("C:\Users\asus567767\CLionProjects\untitled\text", "r", stdin);
        char str[50007][30];
        char s1[200],s2[200];
        int n=0;
        while(~scanf("%s",str[n]))
        {
            insert(str[n]);
            //cout<<str[n]<<endl;
            n++;
        }
        int len;
        int ans;
        for(int i=0;i<n;i++)
        {
            len=strlen(str[i]);
            for(int j=1;j<=len-1;j++){
                strncpy(s1,str[i],j);
                s1[j]='';//!
                strncpy(s2,str[i]+j,len-j);
                s2[len-j]='';//!
                ///cout<<s1<<" "<<s2<<endl;
                ans=find(s1)+find(s2);
                if(ans==2)
                {
                    printf("%s
    ",str[i]);
                    break;
                }
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    数据结构>图的应用(拓扑排序,关键路径) 小强斋
    数据结构>图的最短路径 小强斋
    数据结构>图的连通性和最小生成树 小强斋
    mysql查看表的存储引擎等相关信息
    java.util.TaskQueue的最小堆排序算法的应用
    jetty源码阅读总结1
    lucene3.6.0的高亮显示
    log4j MDC NDC应用场景
    velocity自定义标签和指令
    jetty关于http状态码处理的一个很有用的类
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11386549.html
Copyright © 2011-2022 走看看