• 华农oj Problem B: Averyboy找密码【STL】


    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
    ", v[n>>1]*1.0);
           }
           else{
            printf("%.1f
    ", (v[(n>>1)-1] + v[n>>1])/2.0 );
           }
       }
    }
    
  • 相关阅读:
    leetcode 43. 字符串相乘
    leetcode 20. 有效的括号 (python)
    leetcode 125. 验证回文串(python)
    leetcode 171. Excel表列序号(python)
    leetcode 190. 颠倒二进制位(c++)
    leetcode 122. 买卖股票的最佳时机 II (python)
    leetcode 118. 杨辉三角(python)
    leetcode 141. 环形链表(C++)
    leetcode 189. 旋转数组(python)
    leetcode 217. 存在重复元素 (python)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8996574.html
Copyright © 2020-2023  润新知