[usaco3.1.4]contact

  简单一个暴搜就能过了,但是我的双关键字快排我也是醉了(用c++的sort不会写双关键字排序,只好自己写,可是写得特别拙),代码混乱(看来我应该分开一个个函数写的)

/*
ID:abc31261
LANG:C++
TASK:contact 
*/
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1<<13;
string ss[maxn];
map<string,int> mm;
int len=0,num[maxn],r[maxn];

void dfs(int x,string s2)
{
     if (x==0)ss[++len]=s2;
        else 
        {
             dfs(x-1,s2+'0');
             dfs(x-1,s2+'1');
        }
}

bool check(int x,int y)
{
     if (num[x]==num[y])
     {
        if (ss[x].size()<ss[y].size())return true;
        if (ss[x].size()==ss[y].size() && ss[x]<ss[y])return true;
     }
     return false;
}
bool check1(int x,int y)
{
     if (num[x]==num[y])
     {
        if (ss[x].size()>ss[y].size())return true;
        if (ss[x].size()==ss[y].size() && ss[x]>ss[y])return true;
     }
     return false;
}
void quicksort(int x,int y)
{
     int h=x,l=y,mid=r[(h+l)/2];
     while (h<=l)
     {
           while (num[r[h]]>num[mid] || (check(r[h],mid)))h++;
           while (num[r[l]]<num[mid] || (check1(r[l],mid)))l--;
           if (h<=l)
           {
              swap(r[h],r[l]);
              h++; l--;
           }
     }
     if (h<y)quicksort(h,y);
     if (l>x)quicksort(x,l);
}
int main()
{
    string s="",s1;
    int i,j,l,a,b,n;
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    scanf("%d%d%d",&a,&b,&n);
    while (cin>>s1)s+=s1;
    for (i=a;i<=b;i++)dfs(i,"");                         //构造所有的01序列 
    for (i=1;i<=len;i++)mm[ss[i]]=r[i]=i;                 
    for (i=0;i<=s.size()-a;i++)
    {
        s1="";
        for (j=i;j<i+a-1;j++)s1+=s[j];
        for (j=i+a-1;j<s.size() && j<=i+b;j++)
        {
            s1+=s[j];                                     //逐个枚举                                      
            num[mm[s1]]++;
        }
    }       
    quicksort(1,len);                                     //快排 
    i=0; j=0; 
    while (++j<=n && ++i<=len && num[r[i]]!=0)
    {
          l=1;
          cout<<num[r[i]]<<endl<<ss[r[i]];
          while (i+1<=len && num[r[i+1]]==num[r[i]])
          {
                if (l%6!=0)cout<<" ";                     //输出控制 
                cout<<ss[r[++i]];
                if ((++l)%6==0)cout<<endl;
          }
          if (l%6!=0)cout<<endl;
    }      
    return 0;
}

 

转载于:https://www.cnblogs.com/Sun-Sea/p/5031367.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值