usaco 3.1 Contact

来源:互联网 发布:网络机顶盒软件下载 编辑:程序博客网 时间:2024/06/05 20:56
/*ID:rayafjy1PROG:contactLANG:C++*/#include <iostream>#include <stdio.h>#include <string.h>#include <vector>using namespace std;int ct[13][1 << 13];struct node {    int l, val;};vector<node> output[200005];char str[90], s[200005];int main() {    freopen("contact.in", "r", stdin);    freopen("contact.out", "w", stdout);    int a, b, n, l, i, j, k;    scanf("%d%d%d", &a, &b, &n);    s[0] = '\0';    while (scanf("%s", str) != EOF) strcat(s, str);    memset(ct, 0, sizeof(ct));    l = strlen(s);    for (i = a; i <= b; i++) {        for (j = 0; j < l + 1 - i; j++) {            int sum, tmp;            sum = 0, tmp = 1;            for (k = j + i - 1; k >= j; k--) {                sum += (s[k] - '0') * tmp;                tmp *= 2;            }            ct[i][sum]++;        }    }    memset(output, 0, sizeof(output));    int maxx = 0;    for (i = a; i <= b; i++)        for (j = 0; j < (int)(1 << i); j++) {            node n1;            n1.l = i, n1.val = j;            output[ct[i][j]].push_back(n1);            maxx = max(maxx, ct[i][j]);        }    for (i = maxx; i >= 1; i--) {        if (output[i].size()) {            printf("%d\n", i);            for (j = 0; j < (int)output[i].size(); j++) {                char out[50];                int t1, l1, len = output[i][j].l;                for (k = 0; k < len; k++) out[k] = '0';                out[len] = '\0';                l1 = len - 1;                t1 = output[i][j].val;                while (t1) {                    out[l1--] = t1 % 2 + '0';                    t1 /= 2;                }                printf("%s", out);                if (j == (int)output[i].size() - 1 || (j + 1) % 6 == 0) printf("\n");                else printf(" ");            }            n--;            if (n == 0) break;        }    }    return 0;}

原创粉丝点击