HDU - 3460 - Ancient Printer(字典树)

Ancient Printer

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


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
 

Author
iSea @ WHU
 

Source
 

题意:将n串字符全部打印至少需要多少个步骤

操作有三种:1、将一个字母放入打印机尾部  2、删除打印机尾部的单词 3、打印

先用建字典树,然后对树进行dfs搜索,就可以得到打印的字母总数A。A*2就是打印和删除的总数,因为最后打印的一个单词可以不删除,为了尽量减少操作步骤,最后打印的单词要越长越好,所以最后的答案就是A*2-mx+n (每打印一串要一个print,打印n串就是n个print)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
char s[55];
int n;
struct node
{
    char c;
    vector<node*>next;
    node()
    {
        next.clear();
        c = 0;
    }
}*tree,*add;
void find_and_insert(node *now,char *str,int x,int len)
{
    if(x==len)
        return;
    for(int i=0;i<now->next.size();i++)
    {
        if(now->next[i]->c!=str[x])
            continue;
        find_and_insert(now->next[i], str,x+1,len);
        return;
    }
    add = new node;
    add->c = str[x];
    now->next.push_back(add);
    find_and_insert(add, str,x+1,len);
}
int dfs(node *now)
{
    int ans = 0;
    for(int i=0;i<now->next.size();i++)
        ans += dfs(now->next[i])+1;
    return ans;
}
void delete_tree(node*now)
{
    for(int i=0;i<now->next.size();i++)
        delete_tree(now->next[i]);
    delete now;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        tree = new node;
        int mx = 0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            find_and_insert(tree, s, 0, strlen(s));//建树
            mx = max(mx,(int)strlen(s));
        }
        printf("%d\n",dfs(tree)*2+n-mx);//遍历
        delete_tree(tree);//释放
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值