uva 10602 Editor Nottoobad(字符串 + 排序)

uva 10602 Editor Nottoobad

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


thisthis


Say “delete the last symbol”


0


this thi


Press "n"


1


thisthin


Say “repeat the last word”


0


this thin thin


Press "g"


1


this thin thing

In order to increase the popularity of it’s 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


Sample Output


3

3

this

thin

thing

4

popcorn

apple

apricote

plum

2

hello

hello


6

this

thin

thing

21

popcorn

plum

apricote

apple

5

hello

hello

题目大意:要输入n个单词,现在有三种操作, 1、输入一个字符,需要按下一次按键。  2、删除一个字符,无需按下按键。3、复制一遍单词,无需按下按键。求输出所有单词所需最小的按下按键的次数,并输出该单词序列。

解题思路:每次输入一个单词之后要找另一个与它相似度最近的一个,所以需要先排序。比较两个单词,若后一个单词长则要计算多出部分,否则无需计算。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
string s[105];
int cmp(string a, string b) {
	return a < b;
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			cin >> s[i];
		}
		sort(s, s + n, cmp);
		int len = s[0].size(), j;
		for (int i = 1; i < n; i++) {
			if (s[i][0] != s[i - 1][0]) {
				len += s[i].size();
				continue;
			}
			for (j = 0; j < s[i - 1].size(); j++) {
				if (s[i][j] != s[i - 1][j]) break;
			}
			len += s[i].size() - j;
		}
		printf("%d\n", len);
		for (int i = 0; i < n; i++) {
			cout << s[i] << endl;
		}
	}
	return 0;
}
时间: 2024-11-03 22:30:46

uva 10602 Editor Nottoobad(字符串 + 排序)的相关文章

uva:10602 - Editor Nottoobad(贪心)

题目:10602 - Editor Nottoobad 题目大意:有一个机子它由press的动作还有copy和delete字符的动作.给一组字符串,问要输入这样的一组字符串,最少要执行的press动作. 解题思路:将这一组字符串按照ascall码排序后,这样前后两个字符串的相似度是比较高的.然后后一个字符串和前一个字符串相比,看有多少相同的可以copy,就只要统计一下不相同的字符个数.这题比较迷惑人的是题目一直说要求第一个字符串一定要先执行press动作,但是输出却可以任意给一种,不一定是要第一

UVA 10602 Editor Nottoobad

题意: 有一个产品,可以执行press,repeat,deleltsymbol,给出一串字符,求生成这串字符所用的press功能的最小次数. 思路: 贪心. 所求数目字符串不同字符的总数,所求输出字符是输入字符按字典序排序输出 代码: #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <string> #include <

UVA 156 Ananagrams 关于二维数组表示的字符串排序的问题

题目链接:UVA 156 Ananagrams  Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how

UVA 156-Ananagrams(字符串排序按序输出无重复单词)

Ananagrams Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPO

Openjudge-计算概论(A)-字符串排序

描述 参考整数排序方法,设计一种为字符串排序的算法,将字符串从小到大输出 输入 第一行为测试数据组数t, 后面跟着t组数据.每组数据第一行是n,表示这组数据有n行字符串,接下来是要排序的n行字符串.每行字符串的字符个数不会大于200, n < 100. 输出 对于每组数据,输出排好序的字符串,每组输出后要多输出一个空行 样例输入 2 2 Hello World 4 I Love C Language! 样例输出 Hello World C I Language! Love思路:这题可以把它们全部

gets()、puts()函数。字符串函数。字符串排序的例子。

1.实例程序:string.c的程序: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include<stdio.h> #define MSG "YOU MUST have many talents .tell me some." #define LIM 5 #define LINELEN 81 int main() { char name[LINELEN]; char

leetCode 179. Largest Number 字符串排序 | Medium

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

算法-低位优先的字符串排序

低位优先的字符串排序相当于是对键索引计数方法的一个扩展,主要用于处理固定长度字符串,比如说手机号,固定电话,银行卡卡号,字符串的长度为N,从右向左开始进行每个键作为值开始遍历,实现比较简单: -(void)lowSort:(NSMutableArray *)dataSource singleLength:(NSInteger)len { NSInteger sourceCount=[dataSource count]; NSInteger R=256; NSMutableArray *tempA

关于字符串排序合并的问题

今天遇到了一个问题,题目大意是输入两个字符串,然后给这两个字符串按照ASCII码从小到大进行排序,最后在将两个字符串合并,要求删除其中相同的字符.一开始的时候感觉挺简单的一道题,但是做起来还是小毛病挺多的.还是直接看代码吧,代码里面的注释有许多需要注意的地方. 1 #include<stdio.h> 2 #include<string.h> 3 void sort(char *p) //给字符串排序,参数为字符串首地址 4 { 5 char temp; 6 char *head,*