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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Hat’s Words(字典树)

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hat’s Words(字典树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Hat’s Words

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


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 題解:判斷單詞是否是由兩個單詞合并得到,加了個val判斷是否為一個完整的單詞;本來還以為是只與hat結合吶,神經的只減去hat,誰知道,自己想錯了。。。是兩個單詞的結合; 代碼:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int INF=0x3f3f3f3f;
 9 const double PI=acos(-1.0);
10 const int MAXN=50010;
11 int ch[MAXN][30],word[MAXN],val[MAXN];
12 char dt[50010][110];
13 int sz;
14 void initial(){
15     sz=1;
16     mem(ch[0],0);mem(word,0);mem(val,0);
17 }
18 void join(char *s){
19     int len=strlen(s),k=0,j;
20     for(int i=0;i<len;i++){
21         j=s[i]-'a';
22         if(!ch[k][j]){
23             mem(ch[sz],0);
24         //    val[sz]=0;
25             ch[k][j]=sz++;
26         }
27         k=ch[k][j];
28         word[k]++;
29     }
30     val[k]=1;
31 }
32 bool find(char *s){
33     int len=strlen(s),k=0,j;
34     for(int i=0;i<len;i++){
35         j=s[i]-'a';
36         k=ch[k][j];
37         if(!word[k])return false;
38     }
39     if(val[k])return true;
40     else return false;
41 }
42 int main(){
43     initial();
44     int t=0;
45     char s[110],l[110],r[110];
46     while(~scanf("%s",dt[t]))join(dt[t++]);
47     for(int i=0;i<t;i++){
48         int len=strlen(dt[i]);
49         if(len<=1)continue;
50         for(int j=1;j<len;j++){
51             strcpy(r,dt[i]+j);
52             strcpy(l,dt[i]);
53             l[j]='\0';
54             if(find(l)&&find(r)){
55                 puts(dt[i]);break;
56             }
57         }
58     }
59     return 0;
60 }

?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace  std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1000010;//要開的足夠大 
int ch[MAXN][30];
int word[MAXN];
char str[50010][110];
int val[MAXN]; 
int sz;
int N;
char l[110],r[110];
void insert(char *s){int k=0,j;for(int i=0;s[i];i++){j=s[i]-'a';if(!ch[k][j]){mem(ch[sz],0);ch[k][j]=sz++;}k=ch[k][j];word[k]++;}val[k]=1;
}
bool find(char *s){int k=0,j;for(int i=0;s[i];i++){j=s[i]-'a';k=ch[k][j];if(!word[k])return false;}if(val[k]!=1)return false;return true;
}int main(){int tp=0;sz=1;mem(ch[0],0);mem(val,0);mem(word,0);while(~scanf("%s",str[tp]))insert(str[tp++]);
//	printf("%d\n",tp);for(int i=0;i<tp;i++){for(int j=0;str[i][j];j++){strcpy(l,str[i]);l[j]='\0';strcpy(r,str[i]+j);//		printf("**%s %s\n",l,r);if(find(l)&&find(r)){puts(str[i]);break;}}}return 0;
}

  

轉載于:https://www.cnblogs.com/handsomecui/p/4918496.html

總結

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

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