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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

hdu1247 Hat’s Words

發布時間:2025/6/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu1247 Hat’s Words 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247

題目:

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13765????Accepted Submission(s): 4927


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

?思路:1.用Trie樹,枚舉每個單詞,看他是否由兩個單詞組成,判斷過程其實就是字典樹的查找。時間復雜度是O(n*len)

    2.map爆一發,map哈希的復雜度不清楚,不過也是枚舉每個單詞是否由兩個單詞組成,枚舉過程時間復雜度也是O(n*len),總體復雜度和Trie復雜度應該差不多。

    3.我看到我的運行時間很短,可能暴力枚舉加map也能過。枚舉str[i],str[j],判斷map[str[i]+str[j]]存不存在即可,不知道能過否

代碼:

#include <stdio.h> #include <stdlib.h>using namespace std;#define MAXNUM 26 //定義字典樹結構體 typedef struct Trie {bool flag;//從根到此是否為一個單詞Trie *next[MAXNUM]; }Trie; //聲明一個根 Trie *root; char ss[50006][100]; //初始化該根 void init() {root = (Trie *)malloc(sizeof(Trie));root->flag=false;for(int i=0;i<MAXNUM;i++)root->next[i]=NULL; } //對該字典樹的插入單詞操作 void insert(char *word) {Trie *tem = root;while(*word!='\0'){if(tem->next[*word-'a']==NULL){Trie *cur = (Trie *)malloc(sizeof(Trie));for(int i=0;i<MAXNUM;i++)cur->next[i]=NULL;cur->flag=false;tem->next[*word-'a']=cur;}tem = tem->next[*word-'a'];word++;}tem->flag=true; } bool search2(char *word) {Trie *tem = root;char *p=word;while(*p){if(tem==NULL||tem->next[*p-'a']==NULL)return false;tem=tem->next[*p-'a'];p++;}return tem->flag; } //查詢一個單詞的操作 bool search1(char *word) {Trie *tem = root;for(int i=0;word[i]!='\0';i++){tem=tem->next[word[i]-'a'];if(tem->flag&&search2(&word[i+1]))return 1;}return 0; }int main(void) {int n=1;init();while(~scanf("%s",ss[n]))insert(ss[n]),n++;for(int i=1;i<n;i++)if(search1(ss[i]))printf("%s\n",ss[i]);return 0; }

?

轉載于:https://www.cnblogs.com/weeping/p/5932318.html

總結

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

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