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 }
				
时间: 2024-12-17 03:52:00

H - Hat’s Words HDU - 1247的相关文章

HDU 1247 Hat&#39;s Words (字典树)

[题目链接]click here~~ [题目大意]A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. ,找出由两个子字符串组成的字符串. [解题思路]字典树 #include <bits/stdc++.h> using namespace std; const int N=5*1e4+100; const int MOD=

hdu 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

HDU 1247 Hat’s Words Trie题解

使用Trie的insert函数,主要是要灵活修改search函数,使得其可以快速搜索hat word. 思路: 1 先搜索一个word的前缀有没有在Trie树中找到,如果找到,保存这个Node,方便下面继续搜索, 就搜索余下的能不能是Trie树上的一个单词,如果找到,那么就是hat word了. 2 如果没找到,那么就沿着刚刚搜索到的前缀单词的节点继续往下搜,这样就可以加速程序,不用每次重复搜索. 3 如果沿着Trie树已经走到word的叶子节点了,那么就结束这个word的搜索了. 实现好这些思

hdu 1247 Hat’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a

HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<math.h> #include <iostream> #include<map> using namespace std; string word[50010]; int main() { i

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

HDU 1247 Hat&#39;s words(字典树Trie)

解题思路: 判断给出的单词是否恰好由另外两个单词组成,用栈保存每个子字符串的节点,从这个节点出发判断剩下的字符串是否在字典树中即可. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <sta

hdu 1247 Hat’s Words(从给的单词中找hat&#39;s word 并按字典序输出)

1.在使用mp[key]的时候它会去找键值为key的项,如果没有,他会自动添加一个key的项,再把value赋值为相应的初始值(value是int的话赋值为0,string的话赋值为空).所以如果是插入的话可以用insert,如果是查找的话可以使用find,这样可以节省开销.查找的时间复杂度为O(logn) 2. 代码: #include<iostream> #include<string> #include<map> using namespace std; stri

HDU 1247 Hat&#39;s words(Trie)

HDU 1247 Hat's words(Trie) ACM 题目地址: HDU 1247 Hat's words 题意: 给些单词,问每个单词是否能用另外两个单词拼出. 分析: 直接保存到trie里面,然后暴力切割查询即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1247.cpp * Create Date: 2014-09-24 11:04:11 * Descripton: */ #include <cstdio