USACO contact

   题目意思是给你一个01串, 让你找出其中长度为A-B的串的频率, 输出频率最高的N个, 直接用map搞定, 代码如下:

  

/*
    ID: m1500293
    LANG: C++
    PROG: contact
*/

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <iostream>
#include <vector>

using namespace std;
typedef pair<string, int> PAIR;
vector<PAIR> vec;
map<string, int> strhash;
string input;

int trans(string s)
{
    int res = 0;
    int len = s.length();
    int i = 0;
    while(i < len)
    {
        res = res*2 + s[i]-'0';
        i++;
    }
    return res;
}

bool cmp(const PAIR &a,const PAIR &b)
{
    if(a.second != b.second)
        return a.second > b.second;
    else
    {
        int lena = a.first.length(), lenb=b.first.length();
        if(lena != lenb)
            return lena < lenb;
        int a1=trans(a.first), b1=trans(b.first);
        return a1 < b1;
    }
}

int main()
{

    freopen("contact.in", "r", stdin);
    freopen("contact.out", "w", stdout);
    int A, B, N;
    scanf("%d%d%d", &A, &B, &N);
    string tp;
    input = "";
    while(cin>>tp)
    {
        input += tp;
    }
    //cout<<input<<endl;
    int len = input.length();
    for(int i=0; i<len; i++)
    {
        for(int j=A; j<=B; j++)  if(i+j-1<len)
        {
            string str(input, i, j);
            //cout<<str<<endl;
            if(strhash.find(str) == strhash.end())
            {
                strhash[str] = 1;
            }
            else
                strhash[str]++;
        }
    }
    map<string, int>::iterator it;
    for(it=strhash.begin(); it!=strhash.end(); ++it)
        vec.push_back(*it);
    sort(vec.begin(), vec.end(), cmp);
//    for(int i=0; i<vec.size(); i++)
//    {
//        cout<<vec[i].first<<" "<<vec[i].second<<endl;
//    }

    int idx = 0;
    int vecsize = vec.size();
    for(int i=0; i<N&&idx<vecsize; i++)
    {
        int fre = vec[idx].second;
        cout<<fre<<endl;
        int output = 0;
        int num = 0;
        while(idx<vecsize && fre==vec[idx].second)
        {
            cout<<vec[idx].first;
            num++;
            output++;
            if(output==6)
            {
                output = 0;
                cout<<endl;
            }
            else
            {
                if(idx+1<vecsize && vec[idx+1].second==fre)
                    cout<<" ";
            }
            idx++;
        }
        if(num%6 != 0)
        cout<<endl;
    }
}

 

转载于:https://www.cnblogs.com/xingxing1024/p/5080516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值