HDOJ 1800 Flying to the Mars

点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1800

题目意思:有n个士兵每个人有一个水平值,水平高的的人可以教低的人,意思就是求最少的扫帚,那么我们只要知道找到最大重复元素的次数即可,因为相同的人肯定不能共用一个,所以求得最少即为最大的重复次数

注意:前置的0必须要去掉,例如数据

           3

           0

           00

           000

输出    3

代码1(直接map):
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;

//去掉前置的0

string Str(string str){
     int mark , i;
     string temp;
     if(str.size() == 1)//如果长度为1直接返回
         return str;
     for(i = 0 ; i < str.size() ; i++){
        if(str[i] != '0'){                  
            mark = i;
            break;
        }
     }
     if(i == str.size())//如果 i为 长度说明都是0直接返回一个“0”
         return "0";
     else{
         for(int i = mark ; i < str.size() ; i++)
             temp += str[i];//用temp来存储去0后的字符串
         return temp;
     }
}

int main(){
    int n , max , t;
    string str , temp;         
    map<string , int>mp;
    while(scanf("%d" , &n) != EOF){
        max = 0;
        for(int i = 0 ;i < n ; i++){
            cin>>str;
            temp = Str(str);
            t = ++mp[temp];//注意这里用一个t来存储值,不然超时,map的键值用字符串会很慢
            if(max < t)
                max = t;
        }  
        cout<<max<<endl;
        mp.clear();//每次清除mp对象
    }
    return 0;
}





posted on 2012-06-26 12:57  c语言源码  阅读(194)  评论(0编辑  收藏  举报

导航