hdu 3460 Ancient Printer(trie tree)

来源:互联网 发布:阿里云 云市场 编辑:程序博客网 时间:2024/05/17 08:17

Ancient Printer

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


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
 

Author
iSea @ WHU
 

Source
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU


古代打印机


时间限制:2000/1000 MS(Java / Others)内存限制:131072/65536 K(Java / Others)
总提交次数:1923接受提交:961




问题描述
比赛开始了!在准备比赛时,iSea希望将团队名称分别打印在一张纸上。
不幸的是,iSea可以找到只是一个古老的打印机:这么古老,你不能相信它,它只有三种操作:


●'a' - 'z':可以键入二十六个字母
●'Del':删除最后一个字母(如果存在)
●“打印”:打印在打印机中输入的字词


打印机在开始时是空的,iSea必须使用三个操作打印所有团队的名称,不一定按照输入的顺序。每次,他可以在打印机的末尾键入字母,或删除最后一个字母,或打印当前词。打印后,打印机中的字母静止不动,您可以删除一些字母打印下一个字母,但不需要删除最后一个字母的字母。
iSea想尽量减少操作总数,请帮助他。
 


输入
在输入中有几个测试用例。


每个测试用例以一个整数N(1≤N≤10000)开始,表示团队名称的数量。
然后N个字符串跟随,每个字符串只包含小写,不为空,并且其长度不大于50。


输入终止于文件标记结束。
 


输出
对于每个测试用例,输出一个整数,表示最小操作数。
 


样品输入
2
freeradiant
自由开放
 


示例输出
21
暗示
样品的操作是:
f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-
 
 


作者
iSea @ WHU
 


资源
2010 ACM-ICPC多学科大学培训竞赛(3) - 由WHU主办

点击打开链接

题意:给了我n个字符串,有三个操作,写一个字符,删除一个字符,还有一个打印字符,问最少多少次操作就能完成

思路:对于这个所有字符串形成的字典树来说,有的字符是需要写一次并且要删除一次的,这样的字符称它为节点,设几点个数为alen,然后所有节点的操作就是alen*2,然后每个串完成后要打印,那么加上n,最后减去一个所有串的长度最长的串,那是因为在写最后一个串的时候写完就可以,不用再删除了,所以补回来


////  main.cpp//  160626////  Created by liuzhe on 16/6/26.//  Copyright © 2016年 my_code. All rights reserved.//#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>using namespace std;//hdu 3460 trie treeconst int maxn = 10010;int alen;char str[maxn][55];struct trie{    trie *next[26];    int v;};trie *root;void create_tree(char *str){    int len = strlen(str);    trie *p = root,*q;    for(int i=0;i<len;i++)    {        int id = str[i]-'a';        if(p->next[id]==NULL)        {            q = (trie *)malloc(sizeof(trie));            q->v = 0;            alen++;            for(int i=0;i<26;i++)                q->next[i] = NULL;            p->next[id] = q;        }        p = p->next[id];    }}int delete_tree(trie *t){    if(t==NULL)        return 0;    for(int i=0;i<26;i++)    {        if(t->next[i]!=NULL)            delete_tree(t->next[i]);    }    free(t);    return 0;}int main(int argc, const char * argv[]){    int n;    while(cin>>n)    {        root = (trie *)malloc(sizeof(trie));        for(int i=0;i<26;i++)            root->next[i] = NULL;        int max1 = -1;        alen = 0;        for(int i=0;i<n;i++)        {            scanf("%s",str[i]);            int len1 = strlen(str[i]);            max1 = max(max1,len1);            create_tree(str[i]);        }        printf("%d\n",alen*2+n-max1);        delete_tree(root);    }    return 0;}


0 0
原创粉丝点击