usaco Chapter 3 section 3.1 Contact

/*

Contact
IOI'98

The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right from the centre of the galaxy. They wish to know if the emission is transmitted by some extraterrestrial form of intelligent life or if it is nothing but the usual heartbeat of the stars.

Help the cows to find the Truth by providing a tool to analyze bit patterns in the files they record. They are seeking bit patterns of length A through B inclusive (1 <= A <= B <= 12) that repeat themselves most often in each day's data file. They are looking for the patterns that repeat themselves most often. An input limit tells how many of the most frequent patterns to output.

Pattern occurrences may overlap, and only patterns that occur at least once are taken into account.

*/ 

/*
ID: niepeng1
PROB:contact
LANG: C++
*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <math.h>
#include <string.h>
#include <stdio.h>

using namespace std;
#define Max1  200010
int way[Max1]={0};
long int cond[20][4916];
struct Node{
 int count;//出现频繁度
 int data;//位数
 int jj;
}; 
Node tem[20000];
FILE *in,*out;
int cmp(const Node & a,const Node & b){
 if(a.count==b.count)//频度相同
 {
  if(a.data == b.data)//位数相同,比二进制数大小
   return a.jj<b.jj;
  return a.data<b.data;
 }
 return a.count>b.count;//位数相同比频度
}

void Display(int count,int data,int j)
{
 char a[20]="000000000000000";
 a[data]='/0';
 while(--data>=0)
 {
  a[data]=j&1;
  j=j>>1;
  a[data]+='0';
 }
 fprintf(out,"%s",a);
}
int main()
{
 char rec[Max1];
 int i,j,a,b,num,len,k,id,iu,j1;
 char cc;

 for(i=0;i<20;i++)
  for(j=0;j<4915;j++)
  {
   cond[i][j]=0;
  }
 in=fopen("contact.in","r");
 out=fopen("contact.out","w");
 fscanf(in,"%d %d %d",&a,&b,&num);
 i=0;
 while(fscanf(in,"%c",&cc)!=-1)
 {
  if(cc!='/n')
   rec[i++]=cc;
 }
 rec[i]='/0';
 len=i;
 for(k=0;k<=12;){
  for(i=0;i<len;i++)
  {
   way[i]=way[i]<<1;
   way[i]+=(rec[i+k]-'0');
   if(cond[k+1][way[i]]==0 && k+1 >= a && k+1 <= b)
   {
    j++;
   }
   cond[k+1][way[i]]++;
  }
  k++;
  len--;
 }
    id=pow(2,a);
 iu=pow(2,b);
 k=0;
 for(i=a;i<=b;i++){
  for(j=0;j<4915;j++){
   if( cond[i][j] >0)
   {
    tem[k].data=i;//位数
    tem[k].count=cond[i][j];
    tem[k].jj=j;
    k++;
   }
  }
 }

 sort(tem,tem+k,cmp);
 fprintf(out,"%d/n",tem[0].count);
 int kk=0;
 for(i=0,j1=0;(i<k) & (j1<num);i++){
  Display(tem[i].count,tem[i].data,tem[i].jj);
  if( tem[i].count!=tem[i+1].count)
  {
   if(j1 == num-1 || i == k-1)
   {break;}
   j1++;
   kk=1;
   fprintf(out,"/n%d/n",tem[i+1].count);
  }
  else{
   if( j1 == num || kk%6==0)
    fprintf(out,"/n");
   else
    fprintf(out," ");
   kk++;
  }
 }
 fprintf(out,"/n");
 fclose(in);
 fclose(out);
 return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值