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;
    }
}

 

posted @ 2015-12-27 17:59  xing-xing  阅读(294)  评论(0编辑  收藏  举报