USACO Contact

1、这是98年的一道IOI题目,本来以为要用到KMP,后来发现O(m*n)的算法就可以了。要注意的是0,00,000的二进制值是一样的,为了区分,需要在所有数前面加上一个“1”,这样相当于构建了一个新的映射s->1s,更方便计算(遵循KISS原则:Keep It Simple and Stupid,不要给自己找不痛快)。

2、统计完毕后先按照频率排,频率相同按照长度排(这里可以直接按值的大小,想一想为什么)。要注意每隔6个就要换行,这里很容易错,此外什么时候要输出空格也要注意。

3、加油,马上要到3.2节了!

/*
ID:mrxy564
PROG:contact
LANG:C++
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[200010],ch;
struct node{
    int time;
 int val;
 bool operator <(const node&a)const{
      return time>a.time||time==a.time&&val<a.val;
 }
}a[8200];
int to_digit(int front,int rear){
 int w=1,ans=0;
    for(int i=rear;i>=front;i--){
           ans+=(s[i]-'0')*w;
        w<<=1;
 }
 ans+=w;
 return ans;
}
void to_string(int num){
 int str[15],cnt=0;
    while(num){
     str[cnt++]=num%2+'0';
  num/=2;
 }
 for(int i=cnt-2;i>=0;i--)
  printf("%c",str[i]);
}
int main(){
 freopen("contact.in","r",stdin);
 freopen("contact.out","w",stdout);
 int A,B,N,cnt=0;
 scanf("%d%d%d",&A,&B,&N);
 getchar();
 while((ch=getchar())!=EOF){
   if(ch!='\n') s[cnt++]=ch;
 }
 for(int i=0;i<8200;i++){
  a[i].time=0;
  a[i].val=i;
 }
 for(int i=A;i<=B;i++)
  for(int j=0;j+i-1<cnt;j++){
      int temp=to_digit(j,j+i-1);
      a[temp].time++;
  }
 sort(a,a+8200);
 int temp=-1,c=0,c1=0;
 for(int i=0;i<8200;i++){
  if(a[i].time!=temp){
   if(temp!=-1) printf("\n");
   if((--N)==-1||a[i].time==0) break;
   c1=0;
   printf("%d\n",a[i].time);
   to_string(a[i].val);
   temp=a[i].time;
   c1++;
  }else{
   if(c1==6){
    printf("\n");
       c1=0;
             to_string(a[i].val);
   }else{
       printf(" ");
    to_string(a[i].val);
   }
   c1++;
  }
    }
    return 0;
}

官方题解:

For this problem, we keep track of every bit sequence we see. We could use the bit sequence itself as an index into a table of frequencies, but that would not distinguish between the 2-bit sequence "10" and the 4-bit sequence "0010". To solve this, we always add a 1 to the beginning of the number, so "10" becomes "110" and "0010" becomes "10010".

After reading the entire bit string, we sort the frequency table and walk through it to print out the top sequences.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXBITS 12
#define MAXSEQ (1<<(MAXBITS+1))

typedef struct Seq Seq;
struct Seq {
    unsigned bits;
    int count;
};

Seq seq[MAXSEQ];

/* increment the count for the n-bit sequence "bits" */
void
addseq(unsigned bits, int n)
{
    bits &= (1<<n)-1;
    bits |= 1<<n;
    assert(seq[bits].bits == bits);
    seq[bits].count++;
}

/* print the bit sequence, decoding the 1<<n stuff */
/* recurse to print the bits most significant bit first */
void
printbits(FILE *fout, unsigned bits)
{
    assert(bits >= 1);
    if(bits == 1)	/* zero-bit sequence */
	return;

    printbits(fout, bits>>1);
    fprintf(fout, "%d", bits&1);
}

int
seqcmp(const void *va, const void *vb)
{
    Seq *a, *b;

    a = (Seq*)va;
    b = (Seq*)vb;

    /* big counts first */
    if(a->count < b->count)
	return 1;
    if(a->count > b->count)
	return -1;

    /* same count: small numbers first */
    if(a->bits < b->bits)
	return -1;
    if(a->bits > b->bits)
	return 1;

    return 0;
}

void
main(void)
{
    FILE *fin, *fout;
    int i, a, b, n, nbit, c, j, k;
    unsigned bit;
    char *sep;

    fin = fopen("contact.in", "r");
    fout = fopen("contact.out", "w");
    assert(fin != NULL && fout != NULL);

    nbit = 0;
    bit = 0;

    for(i=0; i<=MAXBITS; i++)
	for(j=0; j<(1<<i); j++)
	    seq[(1<<i) | j].bits = (1<<i) | j;

    fscanf(fin, "%d %d %d", &a, &b, &n);

    while((c = getc(fin)) != EOF) {
	if(c != '0' && c != '1')
	    continue;

	bit <<= 1;
	if(c == '1')
	    bit |= 1;

	if(nbit < b)
	    nbit++;

	for(i=a; i<=nbit; i++)
	    addseq(bit, i);
    }

    qsort(seq, MAXSEQ, sizeof(Seq), seqcmp);

    /* print top n frequencies for number of bits between a and b */
    j = 0;
    for(i=0; i<n && j < MAXSEQ; i++) {
	if(seq[j].count == 0)
	    break;

	c = seq[j].count;
	fprintf(fout, "%d\n", c);

	/* print all entries with frequency c */
	sep = "";
	for(k=0; seq[j].count == c; j++, k++) {
	    fprintf(fout, sep);
	    printbits(fout, seq[j].bits);
	    if(k%6 == 5)
		sep = "\n";
	    else
		sep = " ";
	}
	fprintf(fout, "\n");
    }

    exit(0);
}



 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值