hdu-3460-Ancient Printer(贪心+字典树)

来源:互联网 发布:linux 匹配文件名 编辑:程序博客网 时间:2024/06/05 14:19

Ancient Printer

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


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
2freeradiantfreeopen
 
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
 
题意:这个古老的打印机它只能进行3种操作1.输入二十六个字母2.删除最后一个字母(如果存在)3.打印打印机中打印的单词
每次他可以在打印机末尾输入字母,或者删除最后一个字母,或打印当前单词。打印完毕后,字母正在打印机中,您可以删除一些字母打印下一个字母
输出尽量减少操作总数
思路:
既然输出尽量少的操作次数就要将最长的单词最后打印,用字典树,没添加一个节点数就当成两次操作(输入和删除)然后在加上 n 次次打印的操作数  最后要减去最长的单词
就是结果 因为最后打印的单词肯定是不用删除的。
code;
#include<cstdio>#include<cstring>#include<stdlib.h>using namespace std;int n;struct node{    int cut;    bool flag;    node *next[26];    node(){        cut=0;///与此题无关(原意记录经过每个节点的次数)        flag=false;///同上,可忽略。(原意记录单词的结束)        for(int i=0;i<26;i++){            next[i]=NULL;        }    }};node *root;///根节点(注意一定要在主函数中给它赋值)void del(node *p)///删除字典树,防止超内存{    for(int i=0;i<26;i++){        if(p->next[i]!=NULL) del(p->next[i]);    }    delete(p);}int sum=0;int maxn=0;void Insert(char *str){    int len=strlen(str);    if(len>maxn) maxn=len;///记录最长字符串的长度。    node *p=root;    for(int i=0;i<len;i++){        int id=str[i]-'a';        if(p->next[id]!=NULL){            p=p->next[id];            p->cut++;        }else{            p->next[id]=new node;            sum++;///记录有多少各节点            p=p->next[id];            p->cut=1;        }    }    p->flag=true;}int main(){    while (~scanf("%d",&n)){        root=new node;        char str[100];        for(int i=0;i<n;i++){            scanf("%s",str);            Insert(str);        }        printf("%d\n",2*sum+n-maxn);        sum=0;maxn=0;        del(root);    }    return 0;}