码迷,mamicode.com
首页 > 其他好文 > 详细

*usaco题目分享——Contact

时间:2016-01-24 17:03:16      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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

我是弱渣,我调试了好久(至少有两个小时)......全都是输出格式的错!!!!!!!
其实,只需要一个个读入,再按题目要求做成字符串,还有一些基本字符串的操作。
我们用到了sort神器和map神器。
于是在解决了输出格式后,天都放晴了!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int a,b,n,o;
map<string,int> k;
string g[10005];
int oo;
char s[200555];
struct like
{
    int ti;
    string xu;
    int len;
    int da;
    bool operator < (like l) const
    {
        if(ti==l.ti) 
        {
            if(len==l.len) return da>l.da;
            else return len>l.len;
        }
        else return ti<l.ti;
    }
}f[100005];
int main()
{
    freopen("contact.in","r",stdin);
    freopen("contact.out","w",stdout);
    int i,j;
    scanf("%d%d%d",&a,&b,&n);
    char du;
    while(scanf("%c",&du)==1)
    {
        if(du!=0&&du!=1) continue;
        o++;
        s[o]=du;
        string z="";
        for(i=o;i>=o-b+1;--i)
        {
            string aa="";
            if(i<1) break;
            z+=s[i];
            if(z.length()<a||z.length()>b) continue;
            for(j=z.length()-1;j>=0;--j)
                aa+=z[j];
            if(k[aa]==0)
            {
                oo++;
                g[oo]=aa;
            }
            k[aa]++;
        }
    }
    for(i=1;i<=oo;++i)
    {
        f[i].ti=k[g[i]];
        f[i].len=g[i].length();
        int num=0;
        for(j=0;j<=g[i].length()-1;++j)
            num=num*2+g[i][j]-0;
        f[i].da=num;
        f[i].xu=g[i];
    }
    sort(f+1,f+1+oo);
    int p=0,awa=0;
    i=oo+1;
    int last=0,pp=0;
    while(p<=n)
    {
        i--;
        if(i<1) break;
        if(f[i].ti!=last)
        {
            last=f[i].ti;
            p++;
            if(p>n) break;
            if(awa>0&&pp!=1) printf("\n");
            awa++;
            printf("%d\n",last);
            pp=1;
        }
        if(pp==1)
        {
            cout<<f[i].xu;
        }
        else cout<<" "<<f[i].xu;
        pp++;
        if(pp==7)
        {
            pp=1;
            printf("\n");
        }
    }
    if(pp!=1) printf("\n");
    return 0;
}

 

*usaco题目分享——Contact

标签:

原文地址:http://www.cnblogs.com/hhhhhhhhhh/p/5155350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!