zoukankan      html  css  js  c++  java
  • hdoj 1247 字典树分词 strncpy函数

    Hat’s Words

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


    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<stdio.h>
    #include<malloc.h>
    #include<string.h>
    char a[50001][51];
    struct node
    {
        bool has;
        struct node *next[26];
    };
    void insert(char *str,node *T)
    {
        node *p,*q;
        int i,id,j,len;
        p=T;
        len=strlen(str);
        for(i=0;i<len;++i)
        {
            id=str[i]-'a';
            if(p->next[id]==NULL)
            {
                q=(node*)malloc(sizeof(node));
                for(j=0;j<26;++j)
                {
                    q->next[j]=NULL;
                    q->has=false;
                }
                p->next[id]=q;
            }
            p=p->next[id];
        }
        p->has=true;
        return;
    }
    
    int find(char *str,node *T)
    {
        int i,id,len,flag;
        node *p;
        p=T;
        len=strlen(str);
        flag=0;
        for(i=0;i<len;++i)
        {
            id=str[i]-'a';
            if(p->next[id]!=NULL)
                p=p->next[id];
            else
                return 0;//这里少个返回,调试了半天
        }
        if(p->has)//判定是否有结尾
            flag=1;
        return flag;
    }
    
    
    
    
    int main()
    {
        node *T;
        int k,i,len,j;
        char str1[51],str2[51];
        k=0;
        T=(node*)malloc(sizeof(node));
        for(i=0;i<26;++i)
        {
            T->next[i]=NULL;
            T->has=false;
        }
        while(scanf("%s",a[k])!=EOF)
        {
            insert(a[k++],T);
        /*    if(k==6)
                break;*/
        }
        for(i=0;i<k;++i)
        {
            len=strlen(a[i]);
            for(j=0;j<len;++j)
            {
                memset(str1,0,sizeof(str1));
                memset(str2,0,sizeof(str2));
                strncpy(str1,a[i],j+1);
                strcpy(str2,a[i]+j+1);
        //        printf("%s    %s\n",str1,str2);
                if(find(str1,T)&&find(str2,T))
                {
                    printf("%s\n",a[i]);
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    java语法基础
    HashMap中的put()和get()的实现原理
    理解:o(1), o(n), o(logn), o(nlogn) 时间复杂度
    mongodb去重分页查询支持排序
    elk日志分析系统搭建(window )亲自搭建
    IDEA更改主题插件——Material Theme UI
    css实现图片的瀑布流且右上角有计数
    C# string "yyMMdd" 转DataTime
    Vue.js系列(一):Vue项目创建详解
    VS2017常用快捷键
  • 原文地址:https://www.cnblogs.com/zibuyu/p/3029957.html
Copyright © 2011-2022 走看看