设为首页 加入收藏

TOP

uva:10602 - Editor Nottoobad(贪心)
2015-01-22 21:13:45 来源: 作者: 【 】 浏览:57
Tags:uva:10602 Editor Nottoobad 贪心

题目:10602 - Editor Nottoobad


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


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


代码:

#include 
  
   
#include 
   
     #include 
    
      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; }
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇obj-c编程17:键值观察(KVO) 下一篇c语言中读入带空格的字符串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: