Company Macrohard has released it’s new version of editor Nottoobad, which can understand a few voice commands. Unfortunately, there are only two voice commands that it can understand — “repeat the last word”, “delete the last symbol”. However, when one uses “repeat the last word” the editor inserts a blank that separates the words. But the company claims that it is possible to type much faster — simply by less number of presses. For example, such a phrase like “this thin thing” requires only 6 presses of the keyboard.

Action Number of presses Content of the document
Press “this” 4 This
Say “repeat the last word” 0 this this
Say “delete the last symbol” 0 this thi
Press “n” 1 this thin
Say “repeat the last word” 0 this thin thin
Press “g” 1 this thin thing

In order to increase the popularity of its product the company decided to organize a contest where the winner will be a person who types a given number of words with minimum number of presses. Moreover, the first word must be typed first, and all the others can be typed in arbitrary order. So, if words “‘apple”, “plum” and “apricote” must be typed, the word “apple” must be typed first, and the words “plum” and “apricote” can be switched. And the most important for you — you are going to take part in the contest and you have a good friend in the company, who told you the word which will be used in the contest. You want be a winner J, so you have to write a program which finds the order of the words, where the number of presses will be minimum.
Input
The first line of the input contains the T (1 ≤ T ≤ 15) the number of test cases. Then T test cases follow. The first line of each test contains a number N (1 ≤ N ≤ 100) — the number of words that must be pressed. Next N lines contain words — sequences of small Latin letters, not longer than 100 symbols. Remember that the first word must be pressed first!
Output
The first line of the output contains number X — the minimum number of presses, which one has to do in order to type all the words using editor Nottoobad. Next N lines contain the words in that minimum order.
    If there are several solutions, you can output one of them.
Sample Input
3
3
this
thin
thing
4
popcorn
apple
apricote
plum
2
hello
hello
Sample Output
6
this
thin
thing
21
popcorn
plum
apricote
apple
5
hello
hello

问题链接:UVA10602 Editor Nottoobad
问题简述:输入n个单词,有三种操作, 1.输入一个字符,需要按下一次按键; 2.通过声控删除一个字符;3.通过声控复制一遍上面的单词。最少按多少次键完成输入这些单词?同时输出单词输入的顺序。
问题分析:尽量复制单词,按键次数就少了。为了做到这一点,先对单词进行字典排序,其实就是一种贪心的做法。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10602 Editor Nottoobad */#include <bits/stdc++.h>using namespace std;const int N = 100;
string s[N];int main()
{std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);int t, n;cin >> t;while(t--) {cin >> n;for(int i = 0; i < n; i++)cin >> s[i];sort(s, s + n);int cnt = s[0].size();for(int i = 1; i < n; i++) {int len = s[i].size(), j = 0;while(j < len && s[i][j] == s[i - 1][j]) j++;cnt += len - j;}cout << cnt << endl;for(int i = 0; i < n; i++)cout << s[i] << endl;}return 0;
}

UVA10602 Editor Nottoobad【贪心】相关推荐

  1. uva 10602——Editor Nottoobad

    题意:给定一些单词,然后让打印这些单词,复制前一个单词和删除当前的最后一个字母没有花费,问最小的打印花费. 思路:贪心.排序后,检查当前和上一个单词有多少一样的,这部分的花费可以为0.也就是总的减去这 ...

  2. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

  3. 《算法竞赛入门经典——训练指南》第一章相关内容

    #<算法竞赛入门经典--训练指南>第一章相关内容 希望各位大牛能指导! 红色为已经做了的...黄色背景是还有不懂地方,希望在年前能刷完第一章啊.... 更新版.google上貌似又加了ex ...

  4. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  5. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  6. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...

  7. 2021ICPC(沈阳) - String Problem(后缀树+贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串 sss,对于每个前缀来说,求出字典序最大的子串. 题目分析:看到子串的字典序,感觉能用后缀树来做,参考了一下大佬的赛上代码: 香港中文大 ...

  8. CodeForces - 1607D Blue-Red Permutation(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列,每个数字有一个颜色,如果是蓝色,每次操作则可以减一:如果是红色,每次操作则可以加一. 问有限次操作后,能否将数组变为一个长度为 nnn 的 ...

  9. 2021牛客多校10 - Train Wreck(贪心)

    题目链接:点击查看 题目大意:给出一个合法的括号序列,再给出 nnn 个数字,括号序列对应着入栈和出栈,问是否存在一个合法的顺序,使得 nnn 个数字按照括号序列操作后,每次入栈后,栈中的序列都是不同 ...

最新文章

  1. cocos 时间函数需要什么引用_2021国家公务员考试时间是什么时候 国考备考时间需要多久...
  2. 运维不得不知道的事 数据中心断电如何做
  3. 史上最详细Docker部署Mysql主从复制,带每一步骤图!!!
  4. 推荐一个 Vue3 全家桶 + TS+ Vite2 + element-plus 的网站实战项目
  5. Qt Creator形状
  6. MVC 之 Partial View 用法
  7. mybatis06 增删改差 源码
  8. element-ui upload组件 上传文件类型限制
  9. javascript-抽象工厂模式
  10. JQuery Ajax 在asp.net中使用总结
  11. java对象--多态的好处
  12. 在线电子商务网站 分页 的封装
  13. python模块之wordcloud
  14. Jquery打造AdRotator轮转图片
  15. sass-RGB颜色函数-RGB()颜色函数
  16. CNVD、CNNVD、CICSVD等区别与联系详解
  17. 容器监控实践—CAdvisor
  18. ue4 项目模板_卡牌游戏项目模板
  19. 第二篇第五章防火防烟分区于分隔
  20. Hexo更换主题教程

热门文章

  1. PyQt5将ui文件编译为py文件的方法
  2. IBM TPM2.0 模拟器
  3. 数字高程模型和地图——thematicmapping.org译文(一)
  4. .net 任务调度 mysql_C#/.NET/.NET Core应用程序编程中实现定时任务调度的方法或者组件...
  5. JavaWeb——swagger使用
  6. Genome2D官方博客及教程
  7. Direct3D Frustum裁剪原理
  8. win10改计算机用户名,win10如何改成自己想要的文件夹用户名?
  9. 力扣-图解算法数据结构-剑指 Offer 05. 替换空格
  10. 1025. 除数博弈