Ancient Printer HDU - 3460 贪心+字典树

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.

InputThere 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.

OutputFor 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个打印字符串)个操作
 4 最后可以剩余字符,那肯定是打印完最后一个字符串后就结束,那也就是说减少的就是这个最后打印出字符串的长度
 5 那肯定这个字符串长度越大那就结果就越小
 6
 7 之后每创建一个节点还要考虑删除它的操作,再加上要打印n个字符串。最后减去那个最长字符串长度就好了
 8
 9 */
10 #include <iostream>
11 #include <cstdio>
12 #include <cstring>
13 #include <cstdlib>
14 #include <algorithm>
15 using namespace std;
16 typedef long long ll;
17 const int maxn=26;
18 const int mod=998244353;
19 typedef struct Trie* TrieNode;
20 int result;
21 struct Trie
22 {
23     int sum;
24     TrieNode next[maxn];
25     Trie()
26     {
27         sum=0;
28         memset(next,NULL,sizeof(next));
29     }
30 };
31 void inserts(TrieNode root,char s[55])
32 {
33     TrieNode p = root;
34     int len=strlen(s);
35     for(int i=0; i<len; ++i)
36     {
37         int temp=s[i]-‘a‘;
38         if(p->next[temp]==NULL) p->next[temp]=new struct Trie(),result++;
39         p->next[temp]->sum+=1;
40         p=p->next[temp];
41     }
42 }
43 void Del(TrieNode root)
44 {
45     for(int i=0 ; i<2 ; ++i)
46     {
47         if(root->next[i])Del(root->next[i]);
48     }
49     delete(root);
50 }
51
52 int main()
53 {
54     int n,ans=0,len;
55     char s[55];
56
57     while(~scanf("%d",&n))
58     {
59         ans=0;
60         TrieNode root = new struct Trie();
61         result=0;
62         for(int i=1; i<=n; ++i)
63         {
64             scanf("%s",s);
65             inserts(root,s);
66             len=strlen(s);
67             if(len>ans)
68             {
69                 ans=len;
70             }
71         }
72         printf("%d\n",result*2+n-ans);
73         Del(root);
74     }
75
76     return 0;
77 }

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12001434.html

时间: 2024-09-29 09:31:35

Ancient Printer HDU - 3460 贪心+字典树的相关文章

hdu 2846 Repository 字典树

// hdu 2846 Repository 字典树 // // 题目大意: // // 有n个字符串,m个待询问的字符串,问这些字符串里面以该询问的 // 字符串为子串的字符串有多少个 // // 解题思路: // // 字典树,将字符串的所有子串插入到字典树中,并设立一个No.标识 // 以免重计数.最后查询就好了 // // 感悟: // // 这题的数据量有点大,虽然p是10000,但是长度是20,单个字符串的 // 最大子串数粗略的估计是 20 * 20 ,所以开的空间也要比较大.开始

HDU 1247 简单字典树

Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7359    Accepted Submission(s): 2661 Problem Description A hat’s word is a word in the dictionary that is the concatenation of exactly

HDU 2846 Repository(字典树,标记)

题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一个串分解而来的,保存的是串的编号 num记录数目. //string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[firs

hdu 1979 DFS + 字典树剪枝

http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 373    Accepted Submission(s): 155 Problem Description There is a matrix of 4*4, yo

HDU 2846 Repository (字典树 后缀建树)

Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2932    Accepted Submission(s): 1116 Problem Description When you go shopping, you can search in repository for avalible merchandises

hdu 1298 T9(字典树+DFS)

题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能,根据概率,每按一个按键,输出可能性最高的串.先给定N个单词,以及频率, 然后是Q次询问,每次询问给定一个按按键的顺序,以1为终止. 解题思路:对单词表建立字典树,每个节点有一个经过的频率,这个频率是根据所有经过该节点的单词频率总和.然后 DFS搜索一遍,将答案保存在ans中. #include <cstdio> #include <cstring> #include <algorithm> using

HDU 1671 (字典树统计是否有前缀)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers: 1. Emergenc

[HDU] 4825 (01字典树)

Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧么? Input 输入包含若干组测试数据,每组测试数据

统计难题 HDU - 1251(字典树)

统计难题 HDU - 1251 题目链接:https://vjudge.net/problem/HDU-1251 Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.