zoukankan      html  css  js  c++  java
  • hdu1247

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6422    Accepted Submission(s): 2399


    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
     
    思路:把每个单词分为两部分,判断两部分是否都在已经建好的字典树中
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    #define MAX 50010
    using namespace std;
    char st[MAX][105];
    struct tree
    {
        struct tree *child[26];
        int v;
    }*root;
    void init()
    {
        int i;
        root=(struct tree*)malloc(sizeof(struct tree));
        for(i=0; i<26; i++)
        {
            root->child[i]=0;
            root->v=0;
        }
        return ;
    }
    void build(char *str)
    {
        int id,i,j,len;
        len=strlen(str);
        struct tree *p=root,*q;
        for(i=0; i<len; i++)
        {
            id=str[i]-'a';
            if(p->child[id]==0)
            {
                q=(struct tree*)malloc(sizeof(struct tree));
                for(j=0; j<26; j++)
                    q->child[j]=0;
                p->child[id]=q;
                p=p->child[id];
                p->v=-1;
            }
            else
            {
                p=p->child[id];
            }
        }
        p->v=1;
        return ;
    }
    int find(char *str)
    {
        int len=strlen(str);
        int i,j,id;
        struct tree *p=root;
        for(i=0; i<len; i++)
        {
            id=str[i]-'a';
            if(p->child[id]==0)
                return -1;
            p=p->child[id];
        }
        if(p->v==1)
            return 1;
        else
            return -1;
    }
    int main()
    {
        int cnt,i,j,len;
        char a[105],b[105];
        init();
        cnt=0;
        while(scanf("%s",st[cnt])!=EOF)
        {
            build(st[cnt]);
            cnt++;
        }
        for(i=0; i<cnt; i++)
        {
            len=strlen(st[i]);
            for(j=0; j<len; j++)
            {
                memset( a,'',sizeof(a) );
                memset( b,'',sizeof(b) );
                strncpy(a,st[i],j);
                strncpy(b,st[i]+j,len-j);
                if(find(a)==1&&find(b)==1)
                {
                    printf("%s ",st[i]);
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    laravel 多对多 belonsToMany
    C语言union关键字
    FW:程序在内存的划分(转)
    操作系统:进程/线程同步的方式和机制,进程间通信
    FW:考查嵌入式C开发人员的最好的16道题(转)
    操作系统死锁产生、条件、和解锁
    100层高楼摔2个鸡蛋的问题?
    【转】看完这个你的位运算学得就差不多了
    函数递归的几个例子
    如何查看服务器(linux系统)当前的负载信息(转)
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3566465.html
Copyright © 2011-2022 走看看