UVA 10602 Editor Nottoobad

题意:

  有一个产品,可以执行press,repeat,deleltsymbol,给出一串字符,求生成这串字符所用的press功能的最小次数。

思路: 贪心。 

  所求数目字符串不同字符的总数,所求输出字符是输入字符按字典序排序输出

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
#define MAXN 102

struct Words{
    char str[MAXN];
};

bool cmp(const Words &a,const Words &b){/*重载sort()函数的比较参数,使其对字符串进行排序*/
    if(strcmp(a.str,b.str) > 0)
        return true;
    else
        return false;
}
class Nottoobad{
    private:
        int wordNum;
        int ansNum;
        Words words[MAXN];
    public:
        void init();
        void process();
        void output();
};

void Nottoobad::init(){
    memset(words,0,sizeof(words));
    ansNum = 0;
}
void Nottoobad::process(){
    int cases;
    cin>>cases;
    while(cases--){
        init();
        cin>>wordNum;
        for(int i = 0;i < wordNum;i++){
            cin >> words[i].str;
        }
        sort(words,words+wordNum,cmp);
        for(int i = 0;i < wordNum;i++){
            int lens = strlen(words[i].str);
            int same = 0;//求相邻字符的最长相同长度。如果为0,相当于新键入。
            while(words[i + 1].str[same] != 0&&(words[i].str[same] == words[i + 1].str[same]))
                same++;
            ansNum = ansNum + (lens - same);//统计不同字符总数
        }
        output();
    }
}

void Nottoobad::output(){
    cout<< ansNum<<endl;
    for(int i = 0;i < wordNum;i++)//按排序之后的字符串输出
        cout<<words[i].str<<endl;
}
int main()
{
//    #ifndef ONLINE_JUDGE
//        freopen("D:\\acm.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    Nottoobad nottoobad;
    nottoobad.process();

    return 0;
}
时间: 2024-12-20 13:57:33

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(贪心)

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

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA 题目1223 - Editor(后缀数组求出现次数超过两次的最长子串的长度)

Mr. Kim is a professional programmer. Recently he wants to design a new editor which has as many functions as possible. Most editors support a simple search function that finds one occurrence (or all occurrences successively) of a query pattern strin

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B