1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| ll n, k, cnt; int ch[maxn][30]; int num[maxn], sub[maxn]; ll ans; char val[maxn];
void insert(char *x, int len) { int now = 0; for (int i = 1; i <= len; i++) { int k = x[i] - 'A' + 1; if (!ch[now][k]) { ++cnt; ch[now][k] = cnt; } now = ch[now][k]; num[now]++; } }
void dfs(int now, int deep, int fa) { REPP(i,1,26) { if(ch[now][i] != 0) { dfs(ch[now][i], deep + 1, now); } }
int sz = num[now] + sub[now]; if(sz < 0) { } else { ans += (sz/k)*deep; if(fa!=-1) { sub[fa] += -(sz/k)*k; sub[fa] += sub[now]; } } }
void solve() { ans = 0; cnt = 0; memset(ch,0, sizeof(ch)); memset(num, 0, sizeof(num)); memset(sub, 0, sizeof(sub)); SLLA(n); SLLA(k); REPP(i,1,n) { scanf("%s", val + 1); int len = strlen(val+1); insert(val, len); } dfs(0, 0, -1); printf("%lld\n", ans); }
|