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

 

posted @ 2015-05-10 00:03  小白v  阅读(164)  评论(0编辑  收藏  举报