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

address:http://acm.hdu.edu.cn/showproblem.php?pid=3460

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 只能在 尾部追加 或者删除一个字母
3 打印当前单词
问输入的几个字符串 最少 多少步数可以打印完

贪心+字典树

既然最少步数 ,肯定 在最后一次打印时, 不再需要删除末尾单词操作, 那么肯定 最长的字符串 最后操作。
那么对应的其他 字母 都要 进行 增加和删除2步
同时,我们在打印时 也消耗步数,n个字符串就是n步
先假设 除根节点以为的节点个数为sum (根节点不存储数据), 字符串个数为n,最大字符串长度为Max
那么我们的总步数不就是 假设 每个节点都 进行增加删除操作 + 打印次数 - 最后不需要删除的节点个数
ans = sum*2 + n - Max;

code :

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int sumroot;
struct node
{
    node * child[26];
    node ()
    {
        for (int i=0; i<26; i++)
        {
            child[i] = NULL;
        }
    }
};
node * root;
void build(char * b)
{
    node *p;
    p = root;
    for (int i=0; i<strlen(b); i++)
    {
        int m = b[i]-'a';
        if (p->child[m]==NULL)
        {
            sumroot++;
            p->child[m] = new node;
        }
        p  = p->child[m];
    }
}
int main()
{
    char s[51];
    int N;

    while (cin>>N)
    {
        root = new node;
        int Max=0;
        sumroot = 0;
        for (int i=0 ;i<N; i++)
        {
            memset(s,0,sizeof(s));
            scanf("%s",s);
            if (strlen(s)>Max)
                Max = strlen(s);
            build(s);
        }
        int ans = sumroot*2 + N - Max;
        cout<<ans<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值