• 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;
        }
    }
  • 相关阅读:
    JMeter--聚合报告之 90% Line 正确理解
    jmeter--函数助手对话框之参数详解
    测试理论--如何根据需求设计测试用例
    java jdk 1.6 下载
    linux磁盘满时,如何定位并删除文件
    linux mysql 新增用户 分配权限
    Hibernate 中多对多(many-to-many)关系的查询语句
    springMVC的url-pattern /和/*的区别
    thinkphp多表关联并且分页
    thinkphp 模板里嵌入 php代码
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5080516.html
Copyright © 2020-2023  润新知