USACO--3.1Contact

字符串处理。我是直接用的map记录的所有的子串,然后再从map中取出结果排序,然后就输出。其实这个题都是0-1串,开始的时候我是想着能不能用hash的方法来记录每个子串,但是后面觉得00 000 0000 这些情况不好处理,后来看了官网题解发现我们可以在这个串的最前面的加上一个1然后就可以用二进制hash的思想求解了。
PS:这道题的输出格式蛮坑的。。。。。。

代码如下

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define maxn 1000000

typedef struct
{
    string str;
    int cnt;
}P;
P p[maxn];

bool cmp(P p1,P p2)
{
    int len1=p1.str.size();
    int len2=p2.str.size();
    if(p1.cnt==p2.cnt)
    {
        if(len1==len2)
        {
            return p1.str<p2.str;
        }
        else
        {
            return len1<len2;
        }
    }
    return p1.cnt>p2.cnt;
}

string str,str1;
int main()
{
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    //freopen("lkl.txt","r",stdin);
    map<string,int>M;
    int a,b,n;
    cin>>a>>b>>n;
    str="";
    while(cin>>str1)
    {
        str+=str1;
    }
    int len=str.size();
    for(int i=0;i<len;i++)
    {
        for(int j=a;j<=b&&i+j<=len;j++)
        {
            str1=str.substr(i,j);
            M[str1]++;
        }
    }
    map<string,int>::iterator it;
    int k=1;
    for(it=M.begin();it!=M.end();it++)
    {
        p[k].str=it->first;
        p[k].cnt=it->second;
        k++;
    }
    sort(p+1,p+k,cmp);
    int cnt=1,cur=p[1].cnt,tmp=1;
    cout<<p[1].cnt<<endl;
    cout<<p[1].str;
    for(int i=2;i<k;i++)
    {
         if(p[i].cnt==p[i-1].cnt)
         {
             if(cnt==0)
                cout<<p[i].str;
             else
                cout<<" "<<p[i].str;
             cnt++;
             if(cnt==6)
             {
                cout<<endl;
                cnt=0;
             }
         }
         else
         {
             if(tmp==n)
                break;
             if(cnt!=0)
               cout<<endl;
             cout<<p[i].cnt<<endl;
             cout<<p[i].str;
             cnt=1;
             tmp++;
         }
    }
    if(cnt!=0)
       cout<<endl;
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值