3.1.5 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.

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


/*
ID: makeeca1
PROG: contact
LANG: C++
*/
/*#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int lmin,lmax,n,mmax,now;
string line,s;

struct record{
    int frequency,size;
    bool operator <(const record& a )const{
        return (frequency>a.frequency)||((frequency==a.frequency)&&(size<a.size));
    }
};
record f[5000];
int get(int l,int step){
    string ts=s.substr(l,step);
    int ans=0;
    //for (int i=ts.size()-1;i>=0;i--)
    for (int i=0;i<ts.size();i++)
        ans=ans*2+(ts[i]=='1'?1:0);
    return ans;
}
void print(int num){
    if (f[num].frequency != now) {
        if (num )printf("
");
        if ((!n)) return;
        n--;
        printf("%d
",f[num].frequency);
        now=f[num].frequency;
    }else printf(" ");

    int x=f[num].size;
    string ans="";
    while (x!=0){
        ans=((x&1==1)?"1":"0")+ans;
        x>>=1;
    }    
    ans=ans.substr(1,ans.size()-1);
    cout<<ans;
    
    print(num+1);
}

int main(){
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    scanf("%d%d%d",&lmin,&lmax,&n);s="";
    while (getline(cin,line)) s+=line;
    for (int step=lmin;step<=lmax;step++)
        for (int start=0;start<=s.size()-step;start++){
            int tmp=(1<<step)+get(start,step);
            if (!f[tmp].frequency){
                f[tmp].size=tmp;
                if (tmp>mmax)mmax=tmp;
            }
            f[tmp].frequency++;        
        }        
    sort(f,f+mmax+1);
    now=-1;
    //if (mmax<n)n=mmax;
    print(0);
    return 0;
}
*/
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int a,b,n,t,x,y,val[16385],c[200020];
string tostring(int ord,int length){
    string tempstr;
    int aaa=ord;
    char j[12]={''};
    for(int i=length-1;i>=0;i--){
        if ((aaa&1)==1){j[i]='1';}
        else{j[i]='0';}
        aaa>>=1;
    }
    return tempstr=j;
}
inline int cmp(string a1,string a2){
    if (a1.length() > a2.length() ) return 1;
    if (a1.length() < a2.length() ) return 0;
    return a1.compare(a2);
}
int main()
{
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    std::ios::sync_with_stdio(false);
    char temp='1';
    cin>>a>>b>>n;
    while ( cin>>temp ){
        if (temp==49){
            c[t]=1;
            t++;
        }
        else if (temp==48){
            c[t]=0;
            t++;
        }
    };
    for (int i=a;i<=b;i++)
        for(int j=0;j<t-i+1;j++){
            x=0;
            for(int k=0;k<i;k++)
                (x<<=1)+=c[j+k];
            val[x+(1<<i)]++;
        }
    while(n--){
        int max=0,temp[16385],z=0;
        string output[16384];
        for (int i=0;i<16385;i++)
            if (max<val[i]){max=val[i];}
        if (max==0) return 0;
        for (int i=0;i<16385;i++)
            if (max==val[i]){
                temp[z]=i;
                z++;
                val[i]=0;
            }
        cout<<max<<endl;
        for (int i=0;i<z;i++)
            output[i]=tostring(temp[i],(int)(log(temp[i])/log(2)+0.0001));
        for(int i=0;i<z-1;i++)
            for(int j=0;j<z-i-1;j++)
                if (cmp(output[j],output[j+1])>0) swap(output[j],output[j+1]);
        for(int i=0;i<z-1;i++){
            cout<<output[i];
            if (i%6==5) cout<<endl;
            else cout<<' ';
        }
        cout<<output[z-1]<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/makeecat/p/3289353.html