hdu 3460 Ancient Printer 解题报告

Ancient Printer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1705    Accepted Submission(s): 828


Problem 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

(字典树节点数-1)*2 -------- 输入,删除操作数
字符串数----------printf操作数
最长字符串的长度---------最后一个不需要删除,所以尽量找最大的

所以答案=(字典树节点数-1)*2+字符串数-最长字符串的长度

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

typedef struct zc
{
    int count;//存深度
    struct zc *next[26];
} node;
//新建节点
node *newnode()
{
    node *q;
    q=(node*)malloc(sizeof(node));
    for(int i=0;i<26;i++)
        q->next[i]=NULL;
    return q;
}
//清理树
void Release(node *T)
{
    for(int i=0;i<26;i++)
        if(T->next[i]!=NULL)
            Release(T->next[i]);
    free(T);
}
//建树
void build(node *T,char *s)
{
    node *p;
    p=T;
    int len=strlen(s),k;
    for(int i=0;i<len;i++)
    {
        k=s[i]-'a';
        if(p->next[k]==NULL)
        {
            p->next[k]=newnode();
            p->next[k]->count=p->count+1;
            p=p->next[k];
        }
        else
            p=p->next[k];
    }
}
//节点数
int jds;
//获取最大深度
int getMax(node *T)
{
    jds++;
    int maxx=0;
    node *q;
    q=T;
    for(int i=0;i<26;i++)
    {
        if(T->next[i]!=NULL)
        {
            q=T->next[i];
            T->count=max(getMax(q),T->count);
        }
    }
    return T->count;
}

int main()
{
    int n;
    char s[60];
    while(scanf("%d",&n)!=EOF)
    {
        jds=0;
        node *T;
        T=newnode();
        T->count=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",s);
            build(T,s);
        }
        int m=getMax(T);
        printf("%d\n",(jds-1)*2+n-m);
        Release(T);
    }
    return 0;
}


你可能感兴趣的:(Printer,字典树,Ancient)