集训队专题(1)1005 Ancient Printer


Ancient Printer

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 14
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[/hint]
 

Author
iSea @ WHU
 

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

       此题不仅考验大家的输入输出能力,还考验了大家的数学能力(小编我TT)。

       直观是看,不难想到,没有共同前缀的词,肯定必须把整个词删掉重新输入,尔有共同前缀的词,肯定是先输入短的词,再输入长的词最节省步骤。所以,我们需要在输入的时候就找到最长的词,然后最后输入它。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{node *next[26];};
node *root;
int  Maxsum;
void Insert(char *s)
{
    node *p = root;
    for(int i=0; s[i]; i++)
    {
        int k = s[i] - 'a';
        if(!p->next[k])
        {
            p->next[k] = new node();
            Maxsum += 2;
        }
        p = p->next[k];
    }
}
void Free(node *p)
{
    for(int i=0; i<26; i++)
        if(p->next[i])Free(p->next[i]);
    free(p);
}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        char s[100];
        int i,MaxLen = 0;
        root = new node();
        Maxsum =  N;
        for(i=0; i<N; i++)
        {
            scanf("%s",s);
            Insert(s);
            int len = strlen(s);
            MaxLen = max(MaxLen, len);
        }
        printf("%d\n", Maxsum - MaxLen);
        Free(root);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值