题目大意是给定n个字符串,构造一个包含所有字符串的节点数量最少的自动机
思路:
Trie+Hash
很明显我们就是要把所有等价的子树归为同一个节点。很明显用哈希映射子树即可。映射的时候有三个要素,
第一个就是每个节点是否为一个字符串的末尾,第二个就是每个节点的深度是否相同,第三个是所有子树的
的哈希是否相同。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int SIZE = 26;
typedef unsigned long long LL;
const LL prime = 5882352941LL;
const int maxn = 5000 + 10;
char s[maxn][SIZE * 2];
struct Node
{
Node *child[SIZE];
int val;
Node(){
memset(child, 0, sizeof(child));
val = -1;
}
}*root;
int n;
void insert(char *str){
Node *q = root;
for(char *c = str; (*c); ++c) {
int idx = (int)*c; idx -= 'a';
if(q->child[idx] == NULL) {
q->child[idx] = new Node();
q->child[idx]->val = 0;
}
q = q->child[idx];
}
q->val = 1;
}
LL pow_mod(LL a, int x) {
LL ans = 1;
while(x) {
if(x & 1) ans = ans * a;
a = a * a;
x >>= 1;
}
return ans;
}
typedef pair<LL, int> pli;
struct Ans
{
int dep, Hash, tail;
Ans(int dep, int Hash, int tail) : dep(dep), Hash(Hash), tail(tail) {}
bool operator < (const Ans & rhs) const {
return dep < rhs.dep || (dep == rhs.dep && Hash < rhs.Hash)
|| (dep == rhs.dep && Hash == rhs.Hash && tail < rhs.tail);
}
};
std::vector<Ans> v;
pli dfs(Node *cur, int val){
LL sum = 1;
int des = 0;
for(int i = 0; i < SIZE; ++i) {
if(cur->child[i]) {
pli tmp = dfs(cur->child[i], i);
tmp.first = (tmp.first * pow_mod(prime, i)) + (cur->child[i]->val ? prime : 0);
des = max(des, tmp.second + 1);
sum = (sum + tmp.first);
}
}
v.push_back(Ans(sum, des, cur->val));
return pli(sum, des);
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
root = new Node();
for(int i = 0; i < n; ++i) {
scanf("%s", s[i]);
insert(s[i]);
}
dfs(root, -1);
sort(v.begin(), v.end());
int sum = 1;
// printf("%d\n", v.size());
for(int i = 1; i < (int)v.size(); ++i) {
if(v[i - 1].dep != v[i].dep || v[i - 1].Hash != v[i].Hash || v[i - 1].tail != v[i].tail) sum++;
}
printf("%d\n", sum);
return 0;
}