zoukankan      html  css  js  c++  java
  • UVA 10602

    题意:

      有一个产品,可以执行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;
    }
    Donghua University
  • 相关阅读:
    调整vmware虚拟机硬盘空间的方法
    微型网络IP转换工具
    初观线程(1)
    Linux逻辑盘卷管理LVM详解(转载)
    微型IP转换工具V1.0升级版
    android 模拟器一键root
    android自用小软件"小毛毛起床啦!“<1>
    很久没更新博客了,写下最近的情况
    解决skype突然无法启动的问题(原创)
    OllyDBG处理C++ EH exception异常
  • 原文地址:https://www.cnblogs.com/ohxiaobai/p/4490853.html
Copyright © 2011-2022 走看看