#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
string st;
int ch[100005][26],n,ans,tot;
bool bo[100005];
priority_queue<int>q[100005];
void dfs(int u,int d){
int y;
for (int i=0; i<26; ++i)
if (y=ch[u][i]){
dfs(y,d+1);
if (q[y].size()){
q[u].push(q[y].top());
q[y].pop();
}
}
if (u && !bo[u]){
ans-=q[u].top()-d;
q[u].pop();
q[u].push(d);
}
}
void insert(string s){
int len=s.size();
int u=1;
for (int i=0; i<len; i++){
int c=s[i]-'a';
if (!ch[u][c]) ch[u][c]=++tot;
u=ch[u][c];
}
bo[u]=true;
q[u].push(len);
ans+=len;
}
int main(){
scanf("%d",&n);
for (int i=1; i<=n; i++){
cin>>st;
insert(st);
}
dfs(1,0);
printf("%d",ans);
}
为什么会Segmentation fault啊 样例
3
codeforces
codehorses
code