H - Hat’s Words 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

题目意思:给N个字符串,求出其中有那些字符串可以由二个其他的字符串组成;
解题思路:采用字典树的方法,储存每一个字符串,再将每一个字符串拆分,看能否在字典树中查找到;
  1 #include <string.h>
  2 #include <iostream>
  3 #include<cstdlib>
  4 #define MAX 26
  5 using namespace std;
  6 
  7 typedef struct TrieNode
  8 {
  9     bool isStr;
 10     struct TrieNode *next[MAX];
 11 }Trie;
 12 
 13 void insert(Trie *root,const char *s)
 14 {
 15     if(root==NULL||*s=='\0')
 16         return;
 17     int i;
 18     Trie *p=root;
 19     while(*s!='\0')
 20     {
 21         if(p->next[*s-'a']==NULL)
 22         {
 23             Trie *temp=(Trie *)malloc(sizeof(Trie));
 24             for(i=0;i<MAX;i++)
 25             {
 26                 temp->next[i]=NULL;
 27             }
 28             temp->isStr=false;
 29             p->next[*s-'a']=temp;
 30             p=p->next[*s-'a'];
 31         }
 32         else
 33         {
 34             p=p->next[*s-'a'];
 35         }
 36         s++;
 37     }
 38     p->isStr=true;
 39 }
 40 
 41 int search(Trie *root,const char *s)
 42 {
 43     Trie *p=root;
 44     while(p!=NULL&&*s!='\0')
 45     {
 46         p=p->next[*s-'a'];
 47         s++;
 48     }
 49     return (p!=NULL&&p->isStr==true);
 50 }
 51 
 52 void del(Trie *root)
 53 {
 54     int i;
 55     for(i=0;i<MAX;i++)
 56     {
 57         if(root->next[i]!=NULL)
 58         {
 59             del(root->next[i]);
 60         }
 61     }
 62     free(root);
 63 }
 64 
 65 char s[50005][100];
 66 
 67 int main(int argc, char *argv[])
 68 {
 69 
 70     Trie *root= (Trie *)malloc(sizeof(Trie));
 71     for(int i=0;i<MAX;i++)
 72     {
 73         root->next[i]=NULL;
 74     }
 75     root->isStr=false;
 76 
 77     int sti = 0;
 78     while (cin>>s[sti++])
 79     {
 80         insert(root, s[sti-1]);
 81     }
 82 
 83     for(int i = 0;i < sti;i++)
 84     {
 85         char temp1[100],temp2[100];
 86         int len = strlen(s[i]);
 87         if(len !=1)
 88         {
 89             for(int tt = 1;tt <len;tt++)
 90             {
 91                 for(int j = 0;j <= tt;j++)
 92                 {
 93                     if(j!=tt)
 94                     temp1[j] = s[i][j];
 95                     if(j==tt)
 96                     temp1[j] ='\0';
 97                 }
 98                 for(int j = tt,j1=0;j <=len;j++)
 99                 {
100                     if(j!=len)
101                     temp2[j1++] = s[i][j];
102                     if(j==len)
103                     temp2[j1++] = '\0';
104                 }
105 
106                 if(search(root,temp1)&&search(root,temp2))
107                    {
108                        for(int i0 = 0;i0 <len;i0++)
109                         cout<<s[i][i0];
110                         cout<<endl;
111                         break;
112                    }
113 
114             }
115         }
116 
117     }
118 
119     del(root);
120     return 0;
121 }

 




转载于:https://www.cnblogs.com/a2985812043/p/7375323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值