(trie) UVA

题目链接

将所有的字符串一起依次建立trie树。

在每一次insert的过程中更新答案。每次insert,每走到一个结点(包括根结点)时,一定会与该结点的所有子结点比较一次,之后按照改字符串的字符,走到下一个结点,只与之前也走到该结点的字符串发生第2次比较。其他先前父节点的子结点的字符串,都因为那一次比较就已经出现不同而结束了比较。重复该过程。到最后的叶子结点时循环已经结束,但是注意还需要再加上必定发生的“第二次比较”。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 #define MIN(a,b) (a>b?b:a)
15 #define rank rankk
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=1e5+5;
20 const int INF=1e9+5;
21 const int B=1024;//桶的大小
22 const double M=4e18;
23 const int MAX_NODE=4e6+5;
24 const int sigma_size=80;
25 using namespace std;
26 const int MOD=1e9+7;
27 typedef pair<int,int> pii;
28 const double eps=0.000000001;
29 ll an;
30 struct Trie
31 {
32     int ch[MAX_NODE][sigma_size];
33     int val[MAX_NODE];
34     int num;
35     Trie(){num=1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}
36     int idx(char c)
37     {
38 //        char oh=' ';
39         if(c=='