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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 1247】Hat’s Words(字典树,预处理,tricks)

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1247】Hat’s Words(字典树,预处理,tricks) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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

題目大意:

給你n個按字典序排列的單詞,問你其中的所有能由另外兩個單詞組成的單詞并按字典序輸出(字符串按照字典序讀入的)

解題報告:

? 因為題目說讀入的字符串最多一行(80個字符?反正我設了55個字符過了),所以直接暴力就行了,假設單個字符串平均長度為len,那復雜度是O(50000/len * (len*len)) 也就是O(50000*len),所以題目如果只說了一共50000個字符,沒說每個字符串的長度,那就需要對每個字符處理的時候先預處理出一個東西。

對每個串正著掃一遍?pre[i]存[0,i]有沒有出現(xiàn)過,再對每個串反著在第二棵樹里掃一遍?suf[i]存[i,len-1]有沒有出現(xiàn)過,如果pre[i]&suf[i+1]?那這個串就符合條件

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 50000 + 5; char s[MAX]; int trie[MAX][55]; int cnt[MAX]; char ss[MAX][55];//???? int tot,n; void insert(char str[]) {int rt = 0;int len = strlen(str);for(int i = 0; i<len; i++) {int cur = str[i] - 'a';if(!trie[rt][cur]) trie[rt][cur] = ++tot;rt = trie[rt][cur];}cnt[rt]++; } int ask(char str[]) {int rt = 0;int len = strlen(str);for(int i = 0; i<len; i++) {int cur = str[i] - 'a';if(!trie[rt][cur]) return -1;rt = trie[rt][cur];}return cnt[rt]; } int main() {while(~scanf("%s",s)) {strcpy(ss[++n],s);insert(s);}for(int i = 1; i<=n; i++) {int len = strlen(ss[i]);int flag = 0;for(int j = 0; j<len-1; j++) {char tmp[55];memset(tmp,0,sizeof tmp);for(int k = 0; k<=j; k++) tmp[k] = ss[i][k];//賦值過來 if(ask(tmp) == 1) {memset(tmp,0,sizeof tmp);for(int k = j+1; k<len; k++) tmp[k-(j+1)] = ss[i][k];if(ask(tmp) == 1) {flag = 1;break;}}}if(flag) printf("%s\n",ss[i]);}return 0 ; }

對于字符串處理那一部分,可以用strncpy函數(shù)。

功能:將字符串from 中至多count個字符復制到字符串to中。如果字符串from 的長度小于count,其余部分用'\0'填補。返回處理完成的字符串。

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。