usaco Contact

 题意很简单,给一个长度不大于200000的字符串,求长度在A-B之间的子串出现的频率,输出前N大频率的子串。 

 这题卡了我很久,一直在纠结使用哪种算法,然后为写这题硬是学会了KMP,发现用不上,又去学AC自动机,发现一些细节不好处理,然后终于找到一种简单的算法,哈希。

要申明的是这个代码是借鉴别人的写的,而且借鉴了很多。非原创。

/*
ID: modengd1
LANG: C++
TASK: contact
*/

#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a,b,n;
map<string,int> mm;
vector<pair<int,string> > vv;

bool cmp(pair<int,string> a,const pair<int,string> b)
{
    if(a.first!=b.first)return a.first>b.first;
    else if(a.second.size()!=b.second.size())
        return a.second.size()<b.second.size();
    else return a.second<b.second;
}

int main ()
{
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    scanf("%d %d %d%\n",&a,&b,&n);
    string str;
    char ch;
    while(~scanf("%c",&ch))
    {
        if(ch=='\n')
            continue;
        str.push_back(ch);
    }
    for(int len=a;len<=b;len++)
        for(int j=0;len+j<=str.size();j++)
            mm[str.substr(j,len)]++;

    for(map<string,int>::iterator it=mm.begin();it!=mm.end();++it)
    {
        pair<int,string> p(it->second,it->first);
        vv.push_back(p);
    }
    sort(vv.begin(),vv.end(),cmp);

    int pt=0;
    int last=-1;
    int counter=0;
    int re=0;
    while(pt<vv.size())
    {
        if(vv[pt].first!=last)
        {
            if(re==n)break;
            if(re!=0)cout<<endl;
            re++;
            counter=0;
            cout<<vv[pt].first<<endl<<vv[pt].second.c_str();
            last=vv[pt++].first;
        }
        else
        {
            counter++;
            if(counter%6==0)
            {
                cout<<endl<<vv[pt].second.c_str();
            }
            else
                cout<<" "<<vv[pt].second.c_str();
            pt++;
        }
    }
    cout<<endl;
    return 0;
}

  

转载于:https://www.cnblogs.com/modengdubai/p/4817558.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值