usaco Chapter 3 section 3.1 Contact

来源:互联网 发布:淘宝卖家的流程图 编辑:程序博客网 时间:2024/06/03 11:17

/*

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;

}