zoukankan      html  css  js  c++  java
  • hdu 3460 Ancient Printer

    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
     
     
    题解:刚开始没理解题意,看了题解才知道要干嘛.....    知道题意之后,感觉真的挺水的.....

    选择最长的那个单词在最后一次打印,这样子才能节省最短的步数。

    所以,直接遍历统计Trie树上的字母个数ans, 设最长的单词长度为max,那么答案便是 2*ans+n-max.

    ans要乘2, 是因为打印后还要一个一个删除掉,相当于每个单词走了两次。

    加上n, 就是打印的次数。

    减去maxLen,是因为最后一个单词不需要删除。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <ctype.h>
     7 #include <iomanip>
     8 #include <queue>
     9 #include <stdlib.h>
    10 using namespace std;
    11 
    12 
    13 struct node
    14 {
    15     int count;     
    16     node *next[26];    
    17     node(){          //构造函数
    18         count=0;
    19         memset(next,0,sizeof(next));
    20     }
    21 };
    22 node *root;
    23 int ans;
    24 char s[55];
    25 void insert(char *s)
    26 {
    27     node *p;
    28     p=root;
    29     int len=strlen(s);
    30     for(int i=0;i<len;i++){
    31         int id=s[i]-'a';
    32         if(!p->next[id]){
    33             ans++;
    34             p->next[id]=new node;
    35         }
    36         p=p->next[id];
    37     }
    38 }
    39 int main()
    40 {
    41     int n,i;
    42     while(scanf("%d",&n)!=EOF)
    43     {
    44         root=new node;
    45         ans=0;
    46         int max=0;
    47         for(i=1;i<=n;i++) 
    48         {
    49             scanf("%s",s);
    50             insert(s);
    51             int len=strlen(s);
    52             if(len>max) max=len; 
    53         }
    54         printf("%d
    ",2*ans-max+n);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    VMware Workstation 卸载时卡在“正在卸载网络驱动程序(Virtual Network Editor夯死)”
    Windows 开启 winrm
    【Git】error: RPC failed; HTTP 413 curl 22 The requested URL returned error:413 Request Entity Too Large
    tricks
    MySQL 5.7原生通用二进制格式安装包安装过程
    WebSnapshotsHelper(HTML转换为图片)
    使用两个zTree,调用一个onCheck函数,分别展开不同的节点
    修改Sql Server字段类型
    添加一条公告并标记为已读
    优化部门人员加载缓慢
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5018369.html
Copyright © 2011-2022 走看看