Ancient Printer HDU - 3460 贪心+字典树
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 }
Ancient Printer HDU - 3460 贪心+字典树的更多相关文章
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- hdu 2846(字典树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- three arrays HDU - 6625 (字典树)
three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- *hdu 5536(字典树的运用)
Input The first line of input contains an integer T indicating the total number of test cases. The f ...
- HDU 6625 (01字典树)
题意:给定两个长为n的数组a和b:重新排列a和b,生成数组c,c[i]=a[i] xor b[i]:输出字典序最小的c数组. 分析:将a中的数插入一颗01字典树a中:将b中的数插入一颗01字典树b中: ...
随机推荐
- Python基础语法5-控制流语句
- P2024 [NOI2001]食物链(种类并查集)
题目链接: https://www.luogu.org/problemnew/show/P2024 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 ...
- C#使用struct直接转换下位机数据
编写上位机与下位机通信的时候,涉及到协议的转换,比较多会使用到二进制.传统的方法,是将数据整体获取到byte数组中,然后逐字节对数据进行解析.这样操作工作量比较大,对于较长数据段更容易计算位置出错. ...
- ASP.NET MVC--sqlserver数据库脚本的导入导出
1.右键选择数据库---任务----生成脚本 2.弹出如下框 导出整个表,默认下一步,否则选择特定数据库对象表单选框 3.修改文件名路径,可以保存脚本到制定路径,否则为默认,点击高级进入 要编写脚本的 ...
- 与数论的厮守05:gcd(a,b)=gcd(b,a mod b)的证明
\[设c=gcd(a,b),那么a可以表示为mc,b可以表示为nc的形式.然后令a=kb+r,那么我们就\\ 只需要证明gcd(b,r)=c即可.{\because}r=a-kb=mc-knc,{\t ...
- QQ刷屏助手
本人小学生,求大佬轻点儿 制作原因 原因很简单,还不是想报仇呗! 代码 1 # 原理是先将需要发送的文本放到剪贴板中,然后将剪贴板内容发送到qq窗口 2 # 之后模拟按键发送enter键发送消息 3 ...
- 白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧。
目录 一.导读 三._search api 搜索api 3.1.什么是query string search? 3.2.什么是query dsl? 3.3.干货!32个查询案例! 四.聚合分析 4.1 ...
- 转 jmeter测试手机号码归属地
jmeter测试手机号码归属地 jmeter测试手机号码归属地接口时,HTTP请求有以下两种书写方法: 1.请求和参数一同写在路径中 2.参数单独写在参数列表中 请求方法既可以使用GET方法又可以 ...
- Arduino 上手实战呼吸灯
前言 这篇稿子比以往的时候来的稍晚了一些,望fans们见谅,那即便如此,最终还是姗姗来迟了,公司新一轮战略性部署,被拖出去孵化新产品,开拓新市场去了,手头精力没有那么多了,另外产品一茬接一茬.韭菜一波 ...
- Linux 三剑客之 grep 使用详解
Linux 最重要的三个命令在业界被称为三剑客,它们是:awk.sed.grep.sed 已经在上篇中讲过,本文要讲的是 grep 命令. 我们在使用 Linux 系统中,grep 命令的使用尤为频繁 ...