标签:size htm cin dfs cto push clear source stream
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
int n, tot, dfn[100005], s[100005][26], dep[100005], idx, siz[100005], ans;
bool iss[100005];
char ss[100005];
vector<int> vec[100005];
void insert(){
int u=0;
int len=strlen(ss);
for(int i=0; i<len; i++){
int t=ss[i]-‘a‘;
if(!s[u][t]) s[u][t] = ++tot;
u = s[u][t];
}
iss[u] = true;
}
void dfs(int x, int d){
dep[x] = d;
dfn[x] = ++idx;
siz[x] = 1;
for(int i=0; i<26; i++)
if(s[x][i]){
dfs(s[x][i], d+1);
siz[x] += siz[s[x][i]];
}
}
void faq(int x){
for(int i=0; i<26; i++)
if(s[x][i]){
faq(s[x][i]);
if(vec[x].size()<vec[s[x][i]].size()) swap(vec[x], vec[s[x][i]]);
for(int j=0; j<vec[s[x][i]].size(); j++){
vec[x].push_back(vec[s[x][i]][j]);
push_heap(vec[x].begin(), vec[x].end());
}
vec[s[x][i]].clear();
}
if(!iss[x] && x && vec[x].size()){
pop_heap(vec[x].begin(), vec[x].end());
vec[x].pop_back();
vec[x].push_back(dep[x]);
push_heap(vec[x].begin(), vec[x].end());
}
if(iss[x]){
vec[x].push_back(dep[x]);
push_heap(vec[x].begin(), vec[x].end());
}
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
scanf("%s", ss);
insert();
}
dfs(0, 0);
faq(0);
for(int i=0; i<vec[0].size(); i++)
ans += vec[0][i];
cout<<ans<<endl;
return 0;
}
标签:size htm cin dfs cto push clear source stream
原文地址:https://www.cnblogs.com/poorpool/p/8977367.html