USACO SECTION 3.1 Contact

本文探讨了牛群如何使用射电望远镜探索宇宙,特别关注银河系中心的神秘微波脉冲发射。为了帮助他们识别可能的外星智能生命迹象或常规恒星心跳,提供了一个分析文件中重复模式的工具。该工具专门寻找长度在1至12位之间的重复模式,并按频率降序列出最常见的模式。通过输入限制输出特定数量的最高频率模式,此过程有助于快速筛选和理解数据集的特征。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

PROGRAM NAME: contact

INPUT FORMAT

Line 1:Three space-separated integers: A, B, N; (1 <= N < 50)
Lines 2 and beyond:A sequence of as many as 200,000 characters, all 0 or 1; the characters are presented 80 per line, except potentially the last line.

SAMPLE INPUT (file contact.in)

2 4 10
01010010010001000111101100001010011001111000010010011110010000000

In this example, pattern 100 occurs 12 times, and pattern 1000 occurs 5 times. The most frequent pattern is 00, with 23 occurrences.

OUTPUT FORMAT

Lines that list the N highest frequencies (in descending order of frequency) along with the patterns that occur in those frequencies. Order those patterns by shortest-to-longest and increasing binary number for those of the same frequency. If fewer than N highest frequencies are available, print only those that are.

Print the frequency alone by itself on a line. Then print the actual patterns space separated, six to a line (unless fewer than six remain).

SAMPLE OUTPUT (file contact.out)

23
00
15
01 10
12
100
11
11 000 001
10
010
8
0100
7
0010 1001
6
111 0000
5
011 110 1000
4
0001 0011 1100

   Test 1: TEST OK [0.000 secs, 3816 KB]
   Test 2: TEST OK [0.000 secs, 3816 KB]
   Test 3: TEST OK [0.011 secs, 3816 KB]
   Test 4: TEST OK [0.000 secs, 3816 KB]
   Test 5: TEST OK [0.022 secs, 3816 KB]
   Test 6: TEST OK [0.022 secs, 3816 KB]
   Test 7: TEST OK [0.032 secs, 3816 KB]

/*
ID: conicoc1
LANG: C
TASK: contact
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int text[200001],TextSize;
int count[13][4100];
int Answer[60000][3];
int First;
int A,B,N;  
int temp=0; 
FILE *fin,*fout;

int ChangeFirst()
{
	int pow,sum=0,i;
	for(i=0,pow=1;i<B;i++){
		sum+=(text[B-i-1]-'0')*pow;
		pow*=2;
	}
	return sum;
}

int comp(const void *A,const void *B)
{
	int *P1,*P2;
	P1=(int *)A;
	P2=(int *)B;
	return P2[0]-P1[0];
}

void PrintS(int Digit,int Length)
{
	char P[13];
	int i;
	for(i=0;i<Length;i++){
		P[i]=(Digit>>(Length-i-1))+'0';
		Digit=(Digit & ~(1<<Length-i-1));
	}
	P[i]='\0';
	fprintf(fout,"%s",P);
}

int main()
{
	fin=fopen("contact.in","r");
	fout=fopen("contact.out","w");
	memset(count,0,sizeof(count));
	char c;
	int i=0,j,k;
	fscanf(fin,"%d %d %d",&A,&B,&N);
	while((c=fgetc(fin))!=EOF){
		if(c!='\n')
			text[i++]=c;
	}
   		
	TextSize=i;
	while(B>TextSize)
		B--;
	temp=ChangeFirst();
	count[B][temp]++;
		
	for(i=1;i<=TextSize-B;i++){
		temp=((temp & ~(1<<(B-1)))<<1 ) + (text[i+B-1]-'0');
           		count[B][temp]++;
	}
	

	for(i=B-1;i>=A;i--){
		temp=(temp& ~(1<<i));
		count[i][temp]++;
	}

	for(i=B-1;i>=A;i--){
		for(j=0;j<(1<<(i+1));j++){
			count[i][j>>1]+=count[i+1][j];
		}
	}
	
	for(k=0,i=A;i<=B;i++){
		for(j=0;j<(1<<i);j++){
			Answer[k][0]=count[i][j];
			Answer[k][1]=i;
			Answer[k++][2]=j;
		}
	}

	qsort(Answer,k,sizeof(int)*3,comp);
	

	int flag;
	for(i=0,j=0;i<N&&j<k&&Answer[j][0]!=0;i++,j++){
		fprintf(fout,"%d\n",Answer[j][0]);
		PrintS(Answer[j][2],Answer[j][1]);
		flag=0;
		while(Answer[j][0]==Answer[j+1][0]&&j<k-1){
			j++;
			flag++;
			if(flag%6==0)
				fprintf(fout,"\n");
			else
				fprintf(fout," ");
			PrintS(Answer[j][2],Answer[j][1]);
		}
		fprintf(fout,"\n");
	}
	return 0;
}


用trie据说很快。。但是至今没用过。

这个类似动态规划,先求出长度为B的所有情况

然后递推到A,要注意文本末尾B-1个元素需要额外加上去

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值