usaco-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 Binclusive (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

思路很简单,用map存下字母串对应的位置,给它一个新的位置,用一个cost记录出现的次数,最后排序,注意输出格式。

代码如下:

/*
ID: yizeng21
PROB: contact
LANG: C++
*/
#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
struct hh{
    int xiaobiao,spend;
}r[300000];
string where[300000];
bool cmp(hh i,hh j){
    if(i.spend>j.spend)return 1;
    else if(i.spend==j.spend){
        if(where[i.xiaobiao].length()<where[j.xiaobiao].length())return 1;
        else if(where[i.xiaobiao].length()==where[j.xiaobiao].length()){
            int len=where[i.xiaobiao].length();
            int ans1=0,ans2=0;
            for(int w=0;w<len;w++){
                ans1=ans1*2+where[i.xiaobiao][w]-‘0‘;
            }
            for(int w=0;w<len;w++){
                ans2=ans2*2+where[j.xiaobiao][w]-‘0‘;
            }
            if(ans1>ans2)return 0;
            else return 1;
        }else return 0;
    }else return 0;
}
int cost[300000];
map<string,int>name;
int main(){
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    char str[300000];
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    char q;
    int tot=0;
    int pos=0;
    while(scanf("%c",&q)!=EOF){
        if((q==‘1‘)||(q==‘0‘)){
            str[++tot]=q;
            string h="";
            for(int i=tot;i>=tot-a+2;i--){
                if(i<=0)break;
                h=str[i]+h;
            }
            for(int i=tot-a+1;i>=tot-b+1;i--){
                if(i<=0)break;
                h=str[i]+h;
                if(name[h]==0){
                    name[h]=++pos;
                    where[pos]=h;
                }
                cost[name[h]]++;
            }
        }
    }
    for(int i=1;i<=pos;i++){
        r[i].xiaobiao=i;
        r[i].spend=cost[i];
    }
    sort(r+1,r+pos+1,cmp);
    int printnum=c;
    int last=1;
    int len=pos;
    int biaoji=0;
    while(printnum!=0){
        int asd=0;
        printnum--;
        pos--;
        printf("%d\n",r[last].spend);
        asd++;
        cout<<where[r[last].xiaobiao];
        for(int i=last+1;;i++){
            if(i>len){
                biaoji=1;
                break;

            }
            if(r[i].spend==r[last].spend){
                if(asd%6!=0)cout<<" ";
                cout<<where[r[i].xiaobiao];
                asd++;
                if(asd%6==0)printf("\n");
                last++;
            }else break;
        }
        if(biaoji==1){
            if(asd%6!=0)printf("\n");
            break;
        }
        last++;
        if(asd%6!=0)printf("\n");
        if(pos==0)break;
    }
}
时间: 2024-10-13 18:16:12

usaco-Section 3.1-Contact的相关文章

USACO Section 2.1 Healthy Holsteins

/* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #include <vector> using namespace std; bool compFun(int x, int y) { int temp, i = 0; while (true) { temp = 1 << i; if (temp&x > temp&y) {

USACO Section 2.2 Party Lamps

/* ID: lucien23 PROG: lamps LANG: C++ */ /* * 此题的技巧之处就是需要注意到任何button只要按下2的倍数次就相当于没有按 * 所以其实只需要考虑4个按钮,每个按钮是否被有效按下过一次就好 * 直接使用枚举法,一共只有2^4=16种情况 * 对于每种情况需要知道被按下的有效次数(也就是被按下过的按钮数),必须满足 * (C-有效次数)%2=0才行,这样其他次数才能视为无效 * 然后验证各种情况是否符合要求,将符合要求的情况按序输出即可 */ #inc

USACO Section 2.2 Runaround Numbers

/* ID: lucien23 PROG: runround LANG: C++ */ #include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { ifstream infile("runround.in"); ofstream outfile("runround.out"); if(!infile || !

USACO Section 2.2 Preface Numbering

/* ID: lucien23 PROG: preface LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; int main() { ifstream infile("preface.in"); ofstream outfile("preface.out")

USACO Training Section 3.1 Contact

P2724 联系 Contact 题目背景 奶牛们开始对用射电望远镜扫描牧场外的宇宙感兴趣.最近,他们注意到了一种非常奇怪的脉冲调制微波从星系的中央发射出来.他们希望知道电波是否是被某些地外生命发射出来的,还是仅仅是普通的的星星发出的 题目描述 帮助奶牛们用一个能够分析他们在文件中记下的记录的工具来找到真相.他们在寻找长度在A到B之间(包含A和B本身)在每天的数据文件中重复得最多的比特序列 (1 <= A <= B <= 12).他们在找那些重复得最多的比特序列.一个输入限制告诉你应输出

USACO Section 2.1 Sorting a Three-Valued Sequence

/* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; void exchange(int nums[], int begin, int end, int N, int x); int sum = 0; int main() { ifst

USACO Section 1.1 Your Ride Is Here

原题: Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, how

[IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. ---------------------------------------------------------------------------------- #include<cstdio> #include<iostream> #include<algorithm> #includ

USACO Section 5.3 Big Barn(dp)

USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .dp(i,j)表示以坐标(i,j)为左上角的barn边长最大值,dp(i+1,j),dp(i,j+1)分别表示向右和向下能扩展的最大边长,但是以此为正方形时,右下方的一个格子没有考虑到,所以就+个dp(i+1,j+1).边界为:dp(i,j)=1(i==n-1或j==n-1). -----------

USACO Section 3.3 Camlot

BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个结论?还是usaco数据太弱了?不过看跑出来的时间,全部枚举或许也可以)).一开始觉得挺麻烦的,不过只要思路清晰写起来应该也没多大问题.大概就是这样了. #include<cstdio> #include<iostream> #include<algorithm> #inc