1、概述
Trie樹,又稱字典樹,單詞查找樹或者前綴樹,是一種用於快速檢索的多叉樹結構,如英文字母的字典樹是一個26叉樹,數字的字典樹是一個10叉樹。
我理解字典樹是看了這位大佬博客。還不了解字典樹的可以先進去學習一下
https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html
還有這個講了下為什么用字典樹,和其他的相比優缺點在哪
https://www.cnblogs.com/Allen-rg/p/7128518.html
現在來個題來更進一步了解字典樹吧 ,嘻嘻-_-
Input
Output
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
#include<cstdio> #include<cstring> using namespace std; int top=0; int a[1000001][27]; int sum[1000001]; void insert(char str[]) { int root=0; for(int i=0;str[i]!='\0';i++) { int x=str[i]-'a'; if(!a[root][x]) { a[root][x]=++top; } sum[a[root][x]]++; root=a[root][x]; } } int find(char str[]) { int root=0; for(int i=0;str[i]!='\0';i++) { int x=str[i]-'a'; if(!a[root][x]) return 0; root=a[root][x]; } return sum[root]; } int main() { char str[11]; while(gets(str)!=NULL) { if(strlen(str)==0) break; insert(str); } while(gets(str)!=NULL) { printf("%d\n",find(str)); } }
解釋:字典樹的一些編號什么的解釋在上兩篇博客中都有講到,我這里就不再解釋,在以上代碼中,我們是使用a數組存放字典樹,sum數組存放了每個點節點的時候的兒子數量,也就是以這個節點下的大分支的數量個數,這樣的話,sum的功能我們就能理解啦,就是sum[k] ,以1編號到k編號的這個字符串,sum[k]存放的就是這個字符串的一些東西,以上代碼中我們存的是兒子數,所以就是以這個字符串為前綴的數量,這里我們就能想到這個sum數組不止可以存放前綴兒子數,下面講另外一個應用,也就是上面這個題
思路:之前我們sum數組我們存的是到這個編號為止的字符串的兒子數,而這個題是說到一個字符串到另外一個字符串的映射,我們就可以想到,一個是一個字符串對應一個整數,一個是一個字符串對應一個字符串,我們是不是只要把那個sum[k]的整數換成字符串就可以了呢,答案是肯定的,當然我這里是預先把那些字符串存了下來,然后sum[k]存的是對應於那個字符串數組的一個編號,下面看代碼
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<string> #include<map> #include<algorithm> using namespace std; typedef long long ll; int n,m,top=0; int sum[100001]; char str[101],s[11]; char c[100001][27]; char dic[100001][11]; void insert(char str[],int cnt) { int root=0; for(int i=0;str[i]!='\0';i++) { int x=str[i]-'a'; if(!c[root][x]) c[root][x]=++top; root=c[root][x]; } sum[root]=cnt; } int find(char str[]) { int root=0; for(int i=0;str[i]!='\0';i++) { int x=str[i]-'a'; if(!c[root][x]) return 0; root=c[root][x]; } return sum[root]; } int main() { int cnt=1; while(gets(str)!=NULL) { if(strlen(str)==0) break; sscanf(str,"%s %s",dic[cnt++],s); insert(s,cnt-1); } while(gets(str)!=NULL) { int x=find(str); if(x==0) printf("eh\n"); else printf("%s\n",dic[x]); } }
字典樹可能還有好多騷操作沒學,以后學了之后再更新,23333,^_^