日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1247 Hat’s Words 字典树(Trie树)

發布時間:2023/12/18 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1247 Hat’s Words 字典树(Trie树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HDU 1247 Hat’s Words 字典樹(Trie樹)

字典樹的建立是應該都是一樣的

?

下面是我的做法:

建立完后, 對每一個單詞都進行find_string()判斷是否符合, 分別對其分成兩半, 用j分隔開(左閉右開);

分別find()其子串[0, j+1), [j+1, string_len), 當兩子串都找到后,則輸出此主串, 然后,break;

一個break也讓我WA, 因為如果輸入為:aa aaa aaaaa, 輸出是aaaaa 和 aaaaa兩行.

?

find()中沒有注意到判斷i=j(匹配, 但不一定存在此單詞), 就直接返回EXIST, 又錯了....唉

結果輸入aa aaa aaaaa時輸出了5、6行...

應該是返回current->exist;(此單詞是否存在)...

?

終于A了...^_^

2010-11-20 10:45:08??? Accepted??? 1247??? 31MS??? 7324K??? 3010 B??? C??? Y

?

代碼 #include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define ALPH_LEN 26
#define LEN 50000
#define WORD_LEN 100

const int ZERO = 0;
const char FIRST_CHAR = 'a';
const char EXIST = '1';
const char NON_EXIST = '0';

char word[LEN][WORD_LEN];

/* 字典樹定義 */
typedef
struct node {
struct node *child[ALPH_LEN]; /* 26個元素 */
char exist; /* exist:'1' 表示存在此單詞, '0'不存在 */
}node,
*Node;
Node root;
/* 字典樹根結點(不存儲任何元素) */

void insert(char *str)
{
int i, index, len;

Node current
= NULL, newnode = NULL; /* 當前結點和新增單詞的首元素 */

len
= strlen(str);
if (ZERO == len) /* 單詞長度為0, 無須插入 */
{
return;
}

current
= root; /* 每一次插入都要從根結點開始 */
for (i = 0; i < len; i++)
{
index
= str[i] - FIRST_CHAR; /* 獲取此字母的下標 */
if (current->child[index] != NULL) /* 存在此字母 */
{
/* 修改當前結點 */
current
= current->child[index];
}
else
{
newnode
= (Node) calloc (1, sizeof(node));
if (NULL == newnode)
{
printf(
"空間分配失敗!\n");
exit(
-1);
}

current
->child[index] = newnode; /* 插入新增結點 */
current
= newnode; /* 修改當前結點 */
}
}

current
->exist = EXIST; /* 新增單詞已存在字典樹中 */
}
/* 查找單詞, 存在返回EXIST, 否則返回NON_EXIST */
char find(const char *str, int i, int j)
{
int index;
Node current;

if (i >= j) /* 此單詞不存在 */
{
return NON_EXIST;
}

current
= root; /* 每一次遍歷都要從根結點開始 */
while (i < j)
{
index
= str[i] - FIRST_CHAR;
if (current->child[index] != NULL) /* 當前字符處于字典樹中 */
{
current
= current->child[index]; /* 修改當前位置 */
}
else
{
return NON_EXIST;
}

i
++;
}

return current->exist; /* 是當前單詞存在與否,而不是EXIST!!! */
}
/* 查找符合的主串 */
void find_string(Node root, int len)
{
int i, j, word_len;

for (i = 0; i < len; i++) /* 每一個單詞都判斷一次 */
{
word_len
= strlen( word[i] ); /* 此單詞長度 */
for (j = 0; j < word_len; j++)
{
if (find(word[i], 0, j + 1) == EXIST) /* 判斷前輟 */
{
if (find(word[i], j + 1, word_len) == EXIST) /* 判斷后輟 */
{
printf(
"%s\n", word[i]);
break;
/* 一個break也讓我WA, 因為如果輸入為:aa aaa aaaaa, 輸出是aaaaa 和 aaaaa兩行 */
}
}
}
}
}
/* 釋放內存 */
void release(Node root)
{
int i;

if (NULL == root) /* 此結點為空, 不需要釋放 */
{
return;
}

for (i = 0; i < ALPH_LEN; i++)
{
if (root->child[i] != NULL)
{
release(root
->child[i]);
}
}

free( root );
root
= NULL;
}

int main()
{
int i;
char string[WORD_LEN];

if ((root=(Node) calloc (1, sizeof(node))) == NULL) {
printf(
"空間分配失敗!\n");
exit(
-1);
}
/* 根結點分配空間 */

i
= 0;
while (scanf("%s", string) != EOF)
{
insert(
string ); /* 插入 */
strcpy(word[i],
string);
i
++;
}

find_string(root, i);
/* 查找符合的單詞 */
release( root );
/* 清理資源 */

return 0;
}

?

/*
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
*/

posted on 2010-11-20 10:56 PeckChen 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/xyoung/archive/2010/11/20/1882421.html

總結

以上是生活随笔為你收集整理的HDU 1247 Hat’s Words 字典树(Trie树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。