首页 > 代码库 > USACO 3.2 Contact

USACO 3.2 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

————————————————————————————————————————————————
一道普通的字典树应用,整理一下语言细节……
一是系统自带的类型自己不能再重载运算符了,要再struct一个类型
还有就是这道题的输出格式【如果有人能看我这个蒟蒻的题解的话……】
六个一排,然后短的串在前面,长的串在后面,相同长度的串才是字典序
 1 /*
 2 ID: ivorysi
 3 PROG: contact
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <set>
12 #include <vector>
13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
17 #define inf 0x7fffffff
18 #define MAXN 400005
19 #define ivorysi
20 #define mo 97797977
21 #define ha 974711
22 #define ba 47
23 #define fi first
24 #define se second
25 //#define pis pair<int,string>
26 using namespace std;
27 typedef long long ll;
28 int tree[100005];
29 string s;
30 int a,b,n;
31 struct pis {
32     int first;string second;
33 };
34 bool operator < (pis c,pis d) {
35     if(c.fi!=d.fi) return c.fi<d.fi;
36     else if(c.se.length()!=d.se.length()) return c.se.length() > d.se.length();
37     else return c.se > d.se;
38 } 
39 priority_queue<pis> mq;
40 void dfs(int u,int t,string str){
41     if(t>b) return;
42     if(t>=a && tree[u]!=0) {
43         mq.push((pis){tree[u],str});
44     }
45     dfs(u<<1,t+1,str+"0");
46     dfs(u<<1|1,t+1,str+"1");
47 }
48 void solve() {
49     scanf("%d%d%d",&a,&b,&n);
50     string tm;
51     while(cin>>tm) s+=tm;
52     int t=s.length();
53     xiaosiji(i,0,t) {
54         int tmp=1;
55         int l=min(b,t-i);
56         xiaosiji(j,0,l) {
57             if(s[i+j]==0) tree[tmp<<=1]++;
58             else tree[tmp=(tmp<<1)+1]++;
59         }
60     }
61     dfs(1,0,"");
62     while(n>0) {
63         int cnt=0;
64         pis p=mq.top();mq.pop();
65         printf("%d\n%s",p.fi,p.se.c_str());
66         ++cnt;
67         if(mq.empty()) {puts("");break;}
68         while(mq.top().fi==p.fi) {
69             if(cnt==6) {puts("");cnt=0;}
70             else printf(" ");
71             printf("%s",mq.top().se.c_str());
72             mq.pop();
73             ++cnt;
74         }
75         puts("");
76         --n;
77     }
78 }
79 int main(int argc, char const *argv[])
80 {
81 #ifdef ivorysi
82     freopen("contact.in","r",stdin);
83     freopen("contact.out","w",stdout);
84 #else
85     freopen("f1.in","r",stdin);
86 #endif
87     solve();
88 }

 

 

USACO 3.2 Contact