一 原题
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
二 分析
三 代码
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!
/*
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;
}