USACO 3.1.5 Contact

扫一遍整个串,每进来一个字符,往前扫(i = a to b)个字符,对每一个子字符串,最高位加上1后(防止忽略前导0)转为数字,对应次数加一。

主要是输出格式有点蛋疼,要仔细一点~


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

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int a, b, n, cnt, tot, now, tmp;
char str[200001];
pair<int, int> hash[10001];

void convert(int num, char* ans)
{
    while (num != 1)
    {
        ans[strlen(ans)] = '0' + (num & 1);
        num >>= 1;
    }
    reverse(ans, ans+strlen(ans));
}

int main()
{
    freopen("contact.in", "r", stdin);
    freopen("contact.out", "w", stdout);
    scanf("%d%d%d", &a, &b, &n);
    for (int i = 0;i < 10001;++i) hash[i] = make_pair(0, i);
    while (~scanf(" %c", &str[tot++]))
    {
        for (int i = a;i <= b;++i)
        if (tot >= i)
        {
            now = 1;
            for (int j = tot-i;j < tot;++j)
                now = (now << 1) + str[j] - '0';
            ++hash[now].first;
        }
    }
    sort(hash, hash+10001);
    for (int j = 10000; ;j = tmp-1)
    {
        if (!hash[j].first) break;
        printf("%d\n", hash[j].first);
        tmp = j;
        while (hash[tmp].first == hash[tmp-1].first) tmp--;
        int count = 0;
        for (int i = tmp; i <= j;++i)
        {
            ++count;
            char ans[15] = {'\0'};
            convert(hash[i].second, ans);
            printf("%s", ans);
            if (count % 6 == 0 || i == j) printf("\n");
            else printf(" ");
        }
        ++cnt;
        if (cnt == n) break;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值