uva:10602 - Editor Nottoobad(贪心)

题目:10602 - Editor Nottoobad

题目大意:有一个机子它由press的动作还有copy和delete字符的动作。给一组字符串,问要输入这样的一组字符串,最少要执行的press动作。

解题思路:将这一组字符串按照ascall码排序后,这样前后两个字符串的相似度是比较高的。然后后一个字符串和前一个字符串相比,看有多少相同的可以copy,就只要统计一下不相同的字符个数。这题比较迷惑人的是题目一直说要求第一个字符串一定要先执行press动作,但是输出却可以任意给一种,不一定是要第一个字符串在第一个位置的那种情况。

代码:

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

const int N = 105;
//char first[N];
struct WORDS{

	char str[N];
}words[N];

int cmp (const WORDS & w1, const WORDS & w2) {

	return strcmp(w1.str, w2.str) < 0 ? true :false;
}

int caculate (int n) {

	int sum = 0;
	int len;
	int k;
	for (int i = 1; i < n; i++) {

		len = strlen(words[i].str);
		k = 0;
		while (words[i - 1].str[k]  && words[i - 1].str[k] == words[i].str[k]) {

			k++;
		}
		sum += len - k;
	}
	return sum + strlen (words[0].str);
}

int main () {

	int t;
	scanf ("%d", &t);
	while (t--) {

		int n;
		scanf ("%d", &n);
		for (int i = 0; i < n; i++)
			scanf ("%s", words[i].str);
		//memcpy (first, words[0].str, sizeof (first));
		sort (words, words + n, cmp);
		printf ("%d\n", caculate(n));
		for (int i = 0; i < n; i++)
			printf("%s\n", words[i].str);
	}
	return 0;
}

uva:10602 - Editor Nottoobad(贪心)

时间: 2024-08-09 00:11:03

uva:10602 - Editor Nottoobad(贪心)的相关文章

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", "de

UVA 10602 Editor Nottoobad

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

UVA 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0

UVA 12130 - Summits(BFS+贪心)

UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每个位置有一个值,现在要求出这个图上的峰顶有多少个.峰顶是这样定义的,有一个d值,如果一个位置是峰顶,那么它不能走到不大于该峰顶高度 - d的位置,如果满足这个条件下,并且无法走到更高的山峰,那么它就是峰顶 思路:利用贪心的策略,把所有点丢到优先队列,每次取出最高的峰值开始找,进行广搜,搜的过程中记录下最大值的点的个数,如果这个是峰顶,就加上这个数.判断是不是峰顶的方法为,如果广搜过程中,不会找到一个点的能到的最高峰

uva 1534 - Taekwondo(dp+贪心)

题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数,每个数最多最多选一次,使得这min(n,m)对数ai,bi,ai-bi的绝对值之和最小. 解题思路:贪心,将两组数分别排序,然后dp[i][j]表示i对,匹配到j时候的最优解. #include <cstdio> #include <cstring> #include <cmath> #include &l

UVA 1422 - Processor (二分+贪心+优先队列)

先对开始时间进行排序,在利用优先队列是结束时间早点先出队: 因为时间只有20000,我们可以去枚举每个单位时间,看要给分配给那个任务, 如果某个时间队列中还有结束时间大于枚举的时间,就跳出判断是在mid的右边. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<stdlib.h> #include<math.h> #

uva 1372 - Log Jumping(贪心)

题目链接:uva 1372 - Log Jumping 题目大意:给出n,k,表示有n个木板和长度均为k,现在给出所有木板的左边位置,如果两块木板有重叠,那么就可以在两块木板之间移动,问说最多能形成的最大块数的环. 解题思路:将木块按照左边界排序,每次如果i块板和i-1,i-2有重叠,那么i块板也可以加入环. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; co

uva 10020- Minimal coverage (贪心思想 简单区间覆盖)

题目大意:给出一个范围M,然后给出若干的区间,以0 0 终止, 要求用最少的区间将0 ~M 覆盖,输出最少个数以及方案. 解题思路:典型的区间覆盖问题,算法竞赛入门经典P154上有讲. /*author: charkj_z */ /*time: 0.108s */ /*rank: 674 */ /*为什么不把没用的地方去掉? 因为去掉了我觉得不像我能写出来的*/ /*Ac code : */ #include<stdio.h> #include<string.h> #include

uva 1346 Songs(贪心)

uva 1346 Songs 题目大意:John Doe 是一个著名的DJ,现在他有n首播放个曲, 每首歌曲有识别符key,歌曲长度l,以及播放频率q.想在John Doe 想将磁带上的歌曲重新排列,方便播放,播放所有歌曲有一个复杂度的计算∑(1≤i≤n)q[i] * ( ∑(1≤j≤i)l[j] ), 然后给出S,请输出重新排列后的第S首歌的识别码. 解题思路:贪心. #include<stdio.h> #include<string.h> #include<stdlib.