浅谈字典树

字典树(Trie Tree)是一种非常简单而又常用的数据结构,他就是将字符串插入到一棵树中,从而形成字典。

举个简单的例子,我们有26个小写字母,那么我们在每个树的节点设置26个指针,分别代表指向'a' ~ 'z',这样除了根节点之外的每个结点可以看作是代表一个字母,其所处深度代表这个字符所处字符串的位置。

其操作非常简单,一般静态分配即申请一个较大的空间作为备用,根结点指向第一个空间。当执行插入操作的时候,从根结点依次往下遍历,如果发现对应字母位置的深度已存在该字母的话,我们将指针移到那个位置。倘若没有对应字母的话,我们将分配一个空间给对应字母而产生一个新的结点。搜索同理,当要查找的字符串未遍历完毕而发现当前结点找不到下一个对应字母位置的结点的话,证明树中没有这个串,跳出。当然动态分配也同理。

做个简单的题作为例子:

Flying to the Mars

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Problem Description
浅谈字典树_第1张图片
In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . “Rush … ” one soldier answers. “Shut up ! Do I have to remind you that there isn’t any road to the Mars from here!” PPF replies. “Fly !” another answers. PPF smiles :“Clever guy ! Although we haven’t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed . For example : There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4; One method : C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick. D could teach E;So D E are eligible to study on the same broomstick; Using this method , we need 2 broomsticks. Another method: D could teach A; So A D are eligible to study on the same broomstick. C could teach B; So B C are eligible to study on the same broomstick. E with no teacher or student are eligible to study on one broomstick. Using the method ,we need 3 broomsticks. …… After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.
Input
Input file contains multiple test cases. In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000) Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);
Output
For each case, output the minimum number of broomsticks on a single line.
Sample Input
4
10
20
30
04
5
2
3
4
3
4
Sample Output
1 
2
这个题说明白一点就是让你找相同数字的人的个数,当然前缀0删除。这个题目就可以用字典树来解,代码如下:
/*************************************************************************
	> File Name: Flying_to_the_Mars.cpp
	> Author: ZhangHaoRan
	> Mail: [email protected]
	> Created Time: 2016年06月04日 星期六 20时21分09秒
 ************************************************************************/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
int ans = 0;
char num[35];
int N;
struct node{
    node *nexti[10];
    int v;
}tt[5000001];
node *root;
char tempch[30];
int tot = 1;
void add(char *str){
    int i = 0;
    node *temp = root;
    while(str[i]){
        if(temp -> nexti[str[i] - '0'] == 0){
            temp -> nexti[str[i] - '0'] = &tt[tot ++];
            temp = temp -> nexti[str[i] - '0'];
            temp -> v = 0;
            for(int j = 0; j < 10; j ++){
                temp -> nexti[j] = 0;
            }
        }
        else{
            temp = temp -> nexti[str[i] - '0'];
        }
        i ++;
    }
    temp -> v ++;
    if(temp -> v > ans){
        ans = temp -> v;
    }
}


int main(void){
    root = &tt[0];
    while(cin >> N){
        if(!N){
            cout << 0 << endl;
            continue;
        }
        for(int i = 0; i < 10; i ++){
            root -> nexti[i] = 0;
        }
        ans = 1;
        tot = 1;
        for(int i = 0; i < N; i ++){
            scanf("%s", tempch);
            int len = strlen(tempch);
            int j = 0;
            for(; j < len; j ++){
                if(tempch[j] == '0'){
                    continue;
                }
                else
                    break;
            }
            if(j != len){
                for(int i = 0; j < len; i ++, j ++){
                    num[i] = tempch[j];
                    if(j == len - 1)
                        num[i + 1] = 0;
                }
            }
            else{
                num[0] = '0';
                num[1] = 0;
            }
            add(num);
        }
        cout << ans << endl;
    }
    return 0;
}
在每次插入的时候寻找此前这个串被插入几次。

查看原文: http://chilumanxi.org/2016/06/04/%e6%b5%85%e8%b0%88%e5%ad%97%e5%85%b8%e6%a0%91/

你可能感兴趣的:(浅谈字典树)