Question: give N team names consisting of lowercase letters, print them out with an old printer, and ask at least a few times to press the keyboard (there is no need to enter the order between team names ), this printer can only perform the following three operations:
1. Enter lower-case letters at the end of the existing base;
2. Delete the last letter;
3. Print the existing string.
(1 <= N <= 10000, Team Name Length <= 50)
--> Organize the Trip, record and summarize the number of points sz, and obtain the distance from each node to the root node. the maximum distance from the root node is obtained, the answer is 2 * sz + N-Max.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxw = 50 + 10;int N;char name[maxw];struct node{ int dis; node *next[26]; node(){ dis = 0; memset(next, 0, sizeof(next)); }};struct Trip{ node *root; int Max; int sz; Trip(){ root = new node; Max = -1; sz = 0; } int idx(char c){ return c - 'a'; } void insert(char *s){ node *t = root; int len = strlen(s), i; for(i = 0; i < len; i++){ int c = idx(s[i]); if(!t->next[c]){ t->next[c] = new node; sz++; } t->next[c]->dis = t->dis + 1; t = t->next[c]; } Max = max(Max, t->dis); } void solve(){ printf("%d\n", 2 * sz + N - Max); }};int main(){ while(scanf("%d", &N) == 1){ Trip trip; for(int i = 0; i < N; i++){ scanf("%s", name); trip.insert(name); } trip.solve(); } return 0;}