usaco3.1.4 Contact

11 篇文章 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 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


二 分析

枚举所有串,统计出现次数排序就行了。坑爹的输出要求啊。交了6次。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: contact
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4196 KB]
   Test 2: TEST OK [0.000 secs, 4196 KB]
   Test 3: TEST OK [0.000 secs, 4196 KB]
   Test 4: TEST OK [0.011 secs, 4196 KB]
   Test 5: TEST OK [0.151 secs, 4516 KB]
   Test 6: TEST OK [0.205 secs, 4716 KB]
   Test 7: TEST OK [0.259 secs, 4716 KB]

All tests OK.

Your program ('contact') produced all correct answers! This is your submission #6 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:contact
*/

#include<iostream>
#include<fstream>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int a, b, n;
string s;
map<string, int> m;

struct node {
    string key;
    int val;
    bool operator < (const node &obj) const {
        if(val == obj.val) {
            if(key.length() == obj.key.length())
                return key < obj.key;
            else
                return key.length() < obj.key.length();
        }
        return val > obj.val;
    }
};
vector<node> v;

int main() {
    ifstream fin;
    fin.open("contact.in");
    ofstream fout;
    fout.open("contact.out");
    fin >> a >> b >> n;
    string line;
    while(fin >> line) {
        s = s + line;
    }
    
    for(int i = 0; i < s.length(); i++) {
        for(int j = a; j <= b; j++) {
            if(i < j - 1) break;
            string tmp = s.substr(i - j + 1, j);
            map<string, int>::iterator it = m.find(tmp);
            if(it != m.end())
                it->second++;
            else
                m.insert(make_pair(tmp, 1));
        }
    }

    map<string, int>::iterator it = m.begin();
    while(it != m.end()) {
        node new_ele;
        new_ele.key = it->first;
        new_ele.val = it->second;
        v.push_back(new_ele);
        it++;
    }
    sort(v.begin(), v.end());
    
    int cnt = 1, last_val = v[0].val, num = 1;
    fout << v[0].val << endl << v[0].key;
    for(int i = 1; i < v.size(); i++) {
        if(v[i].val == last_val) {
            num++;
            if(num % 6 != 1) fout << " ";
            fout << v[i].key;
            if(num % 6 == 0) fout << endl;
        }
        else {
            last_val = v[i].val;
            cnt++;
            if(cnt > n) {
                break;
            }
            if(num % 6 != 0) fout << endl;
            num = 1;
            fout << v[i].val << endl << v[i].key;
        }
    }
    if(num % 6 != 0) fout << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值