HDU - 3460 Ancient Printer(字典树)

HDU - 3460
Ancient Printer
Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separately on a single paper. 
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations: 

● 'a'-'z': twenty-six letters you can type 
● 'Del': delete the last letter if it exists 
● 'Print': print the word you have typed in the printer 

The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters. 
iSea wanted to minimize the total number of operations, help him, please.
 

Input

There are several test cases in the input. 

Each test case begin with one integer N (1 ≤ N ≤ 10000), indicating the number of team names. 
Then N strings follow, each string only contains lowercases, not empty, and its length is no more than 50. 

The input terminates by end of file marker. 
 

Output

For each test case, output one integer, indicating minimum number of operations.
 

Sample Input

         
         
         
         
2 freeradiant freeopen
 

Sample Output

         
         
         
         
21

Hint

The sample's operation is: f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-Print 

/*
Author: 2486
Memory: 29516 KB		Time: 46 MS
Language: G++		Result: Accepted
*/
//题意分析:
//输出的次数一定为n
//最后可以保留一个单词而没必要删除它
//其他的节点都被输入了一次,和删除了一次,而这个单词的节点只被输入了,因为最后可以不用删除减少操作
//因为可以不按照顺序输出,其中肯定是将单词最长的留在打印机里,不将他删除的选择是最好的,所以
//答案 = (字典树节点数 - 1) * 2 + 字符串数 - 最长字符串的长度
//所谓的字典树的节点包括他们的顶点,在此处是应该去除
//所以答案 = 字典树中有字符的节点数 * 2 + 字典串数 - 最长字符串的长度
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXM = 1e4 + 5;
int N, tot, Max;
char str[MAXM][50 + 5];

struct node {
    int d,next[30];
    void init() {
        d = -1;
        memset(next, -1, sizeof(next));
    }
} L[MAXN];

void add(char * a, int len) {
    int now = 0;
    Max = max(Max, len);
    for(int i = 0 ; i < len; i ++) {
        int tmp = a[i] - 'a';
        int next = L[now].next[tmp];
        if(next == -1) {
            next = ++ tot;
            L[next].init();
            L[now].next[tmp] = next;
        }
        now = next;
    }
    L[now].d = 0;
}


int main() {
    while(~ scanf("%d", &N)) {
        tot = Max = 0;
        L[0].init();
        for(int i = 0 ; i < N; i ++) {
            scanf("%s", str[i]);
            add(str[i], strlen(str[i]));
        }
        printf("%d\n", N + 2 * tot - Max);
    }
    return 0;
}




你可能感兴趣的:(HDU - 3460 Ancient Printer(字典树))