SRM 574 250 DIV2

菜鸟的代码:

import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;


public class CityMap
{
    public String getLegend(String[] cityMap, int[] POIs)
    {
        int i,j;
        int[] count = new int[256];
        char ch;
        StringBuilder sb = new StringBuilder();
        
        for(i=0;i<cityMap.length;i++){
            for(j=0;j<cityMap[i].length();j++){
                ch= cityMap[i].charAt(j);
                if(ch!='.')
                    count[ch]++;
            }
        }
        for(i=0;i<POIs.length;i++){
            for(j=0;j<256;j++){
                if(count[j]==POIs[i])    
                    sb.append((char)j);
            }
        }
        
        return sb.toString();
    }
    

}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!

 大神的代码(java):

import java.util.HashMap; 

public class CityMap { 

  public String getLegend(String[] c, int[] p) { 
    int[] a = new int[26]; 
    for(String s: c)  
      for(char i: s.toCharArray()) 
        if(i!='.')a[i-'A']++; 
    HashMap<Integer, String> map = new HashMap<Integer, String>(); 
    for(int i=0; i<a.length;i++) 
      map.put(a[i], ""+(char)('A'+i)); 
    String o=""; 
    for(int i: p) o+=map.get(i); 
     
    return o; 
  } 

}

大神的代码(C++):

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
 
using namespace std;
 
#define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int(i)=(n)-1;(i)>=0;(i)--)
#define SZ(c) ((int)(c).size())
#define ITER(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define FIND(x,c) ((c).find((x))!=(c).end())
 
 
class CityMap 
{
public:
  string getLegend(vector <string>, vector <int>);
};
 
string CityMap::getLegend(vector <string> cityMap, vector <int> POIs) 
{
      string ret="";
    int a[30] = {0};
    bool b[30] = {0};
    REP(i,SZ(cityMap))
    {
        string temp = cityMap[i];
        REP(j,SZ(temp))
        {
            if(temp[j]!='.')
                a[temp[j]-'A']++;
        }
    }
 
    REP(i,SZ(POIs))
    {
        int j;
        for(j=0 ; j<26 ; j++)
            if(a[j]==POIs[i] && !b[j])
            {
                ret+='A'+j;
                b[j]=true;
                break;
            }
    }
 
    return ret;
}
 
 
//Powered by [KawigiEdit] 2.0!

 

 分析:

  算法:数数

  对比:

    1.C++版的大神和我做得差不多。

    2.JAVA版的大神灵活地利用了HashMap,比较有意思,学习了哈。然后,这里明明可以不用StringBuilder的。

    3.从空间复杂度的角度来考虑的话,最好还是申请长度是26的数组。虽然长度是256的时候真的很方便啊,尤其是大小写都存在的时候。

  总结:

    HashMap还是很不错的,学习了。

原文地址:https://www.cnblogs.com/wang3/p/3161814.html