码迷,mamicode.com
首页 > 其他好文 > 详细

uva:10602 - Editor Nottoobad(贪心)

时间:2014-07-08 19:33:44      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:blog   http   2014   for   io   代码   

题目: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(贪心),布布扣,bubuko.com

uva:10602 - Editor Nottoobad(贪心)

标签:blog   http   2014   for   io   代码   

原文地址:http://blog.csdn.net/u012997373/article/details/37093371

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!