1. 程式人生 > >華農oj Problem B: Averyboy找密碼【STL】

華農oj Problem B: Averyboy找密碼【STL】

typedef begin hand 密碼 esp TP esc 1.0 bitset

Problem B: Averyboy找密碼
Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 83  Solved: 29
[Submit][Status][Web Board]
Description
Averyboy獲得了一個串只由大小寫字母組成的密碼,他現在要想辦法解開密碼的key,這個密碼的key就是其中每個字母出現的次數的中位數。他現在重金求key,你能幫助他嗎?


Input
第一行一個數字T代表測試的組數。(T<=10)
對於每組測試一行只由大小寫字母組成字符串s(0<|s|<=2000)


Output
對於每組數據輸出一個數字代表字符串中每個字母出現的次數的中位數,每個數字占一行。(小數點後保留一位)

Sample Input
4
Averyboyishandsome
hehee
zuomengba
abbccc
Sample Output
1.0
2.5
1.0
2.0
HINT

第4組樣例中,其中a出現了1次,b出現了2次,c出現了3次,中位數是2

【分析】:註意這道題對大小寫敏感,大小寫是不同的。所以hash好像不行,要用map。

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long ll;
typedef pair <int, int> PLL;
int a[10005];
vector<int> v;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {
       map<char,int> mp;
       mp.clear();
       v.clear();
       memset(a,0,sizeof(a));
       int cnt=0;
       string s;

       cin>>s;
       for(int i=0;i<s.size();i++)
       {
           mp[s[i]]++;
       }
       for(char i='a';i<='z';i++)
       {
           if(mp[i]!=0)
              v.push_back(mp[i]);
       }
       for(char i='A';i<='Z';i++)
       {
           if(mp[i]!=0)
              v.push_back(mp[i]);
       }
       sort(v.begin(),v.end());
       int n = v.size();
       //cout<<n<<endl;
       if(n&1==1){
        printf("%.1f\n", v[n>>1]*1.0);
       }
       else{
        printf("%.1f\n", (v[(n>>1)-1] + v[n>>1])/2.0 );
       }
   }
}

華農oj Problem B: Averyboy找密碼【STL】