HDU1247Hat’s Words

题目链接:HDU1247

Hat’s Words

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


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
 


题意:找出可以被拆成2个串的串,这两个串肯定要在给出串的范围内,还有就是要注意可以是同一个串。

题目分析:字典树的模板改一改就可以了,不过不知道为啥我写的程序就是无限WA,调了一整天最终无奈放弃,在网上照一个巨巨的代码修改一下,然后过了。。。

AC代码:

#include <iostream>
#include <cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct node {
    int flag;
    int buf;
    node *next[26];
}trienode;
trienode *root;
char s[50015][100];
void insert_node(char *s)
{
    trienode *t,*h=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(h->next[buf]==NULL)
        {
            t=new node;
            for(int j=0;j<26;j++)
                t->next[j]=NULL;
            t->flag=0;
            h->next[buf]=t;
            h=t;
        }
        else h=h->next[buf];
    }
    h->flag=1;
}

void del(trienode *r)
{
    for(int i=0;i<26;i++)
    {
        if(r->next[i]) del(r->next[i]);
    }
    free(r);
}

int find(char *t)
{
    trienode *r=root;
    int len1=strlen(t);
    int l=0;
    for(l=0;l<len1;l++)
    {
        int buf1=t[l]-'a';
        if(r->next[buf1]!=NULL)
        {
            r=r->next[buf1];
        }
        else return 0;
    }
    return r->flag;
}

int main()
{
    char t[100];
    root=new node;
    for(int i=0;i<26;i++)
        root->next[i]=NULL;
    root->flag=0;
    int k=0;
    while(~scanf("%s",t))
    {
        strcpy(s[k++],t);
        insert_node(t);
    }
    for(int i=0;i<k;i++)
    {
        int len=strlen(s[i]);
        int j=1;
        //int flag1=0;
        if(strcmp(s[i],s[i+1])==0) continue;
        for(j=1;j<len;j++)
        {
            char t1[100],t2[100];
            strcpy(t1,s[i]);
            t1[j]='\0';
            strcpy(t2,s[i]+j);
            if(find(t1)&&find(t2))
            {
                printf("%s\n",s[i]);
                break;
            }
        }
    }
    del(root);
}

这是无限WA的代码。。。。还请大家帮我找找到底哪出问题了,卡在31msWA了。

//
//  main.cpp
//  HDU1247a
//
//  Created by teddywang on 16/3/29.
//  Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct node {
    int flag;
    int buf;
    node *next[26];
}trienode;
trienode *root;
char s[50015][100];
void insert_node(char *s)
{
    trienode *t,*h=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(h->next[buf]==NULL)
        {
            t=new node;
            for(int j=0;j<26;j++)
                t->next[j]=NULL;
            t->flag=0;
            h->next[buf]=t;
        }
        h=h->next[buf];
    }
    h->flag=1;
}

void del(trienode *r)
{
    for(int i=0;i<26;i++)
    {
        if(r->next[i]) del(r->next[i]);
    }
    free(r);
}

int main()
{
    root=new node;
    for(int i=0;i<26;i++)
        root->next[i]=NULL;
    int k=0;
    while(~scanf("%s",s[k]))
    {
        insert_node(s[k++]);
    }
    for(int i=0;i<k;i++)
    {
        trienode *h=root;
        int len=strlen(s[i]);
        int j=0;
       // if(strcmp(s[i],s[i+1])==0) continue;不知道该不该加
        for(j=0;j<len;j++)
        {
            int buf=s[i][j]-'a';
            if(h->next[buf]!=NULL&&h->flag==0)
            {
                h=h->next[buf];
            }
            else
            {
                if(j==len) break;
                h=h->next[buf];
                int flag=0;
                trienode *r=root;
                int len1=len-j;
                int l=0;
                for(l=0;l<len1;l++)
                {
                    int buf1=s[i][j+l]-'a';
                    if(r->next[buf1]!=NULL)
                    {
                        r=r->next[buf1];
                    }
                    else break;
                }
                if(l==len1)
                {
                    if(r->flag==1)
                        flag=1;
                    else flag=0;
                }
                if(flag==1)
                {
                    printf("%s\n",s[i]);
                    break;
                }
            }
        }
    }
    del(root);
}


你可能感兴趣的:(字典树,wa)