答案=字典树节点个数+单词个数-最长的单词的长度。表示不会推倒。看了大牛博客。
#include#include #include #include using namespace std;struct shu{ int nn[27]; }node[500010];int ii, i, n, m, zz, tott, hh;char s[10000];int main(){ while (~scanf("%d", &n)) { for (i = 0; i <= 500000; i++) memset(node[i].nn, -1, sizeof(node[i].nn)); tott = 1; hh = -1; for (ii = 0; ii < n; ii++) { scanf("%s", s); int len = strlen(s); if (len>hh) hh = len; zz = 0; for (i = 0; s[i]; i++) { if (node[zz].nn[s[i] - 'a'] == -1) { node[zz].nn[s[i] - 'a'] = tott; tott++; } zz = node[zz].nn[s[i] - 'a']; } } printf("%d\n", (tott - 1) * 2 + n - hh); } return 0;}