Posted on 2009-04-28 09:34
superman 阅读(85)
评论(0) 编辑 收藏 引用 所属分类:
USACO
1 #include <queue>
2 #include <iostream>
3
4 using namespace std;
5
6 int binaryStr2num(const string & str, int s, int t)
7 {
8 int n = 0;
9 for (int i = s; i < t; i++)
10 n = n * 2 + str[i] - '0';
11 return n;
12 }
13
14 string num2binaryStr(int k, int n)
15 {
16 string s;
17 while (n)
18 {
19 s += (n % 2 + '0');
20 n /= 2;
21 }
22 while (s.size() < k)
23 s += '0';
24 for (int i = 0; i < s.size() / 2; i++)
25 swap(s[i], s[s.size() - i - 1]);
26 return s;
27 }
28
29 int a, b, n, cnt[12 + 1][4096];
30 string s;
31
32 int main()
33 {
34 freopen("contact.in", "r", stdin);
35 freopen("contact.out", "w", stdout);
36
37 cin >> a >> b >> n;
38
39 string ts;
40 while (cin >> ts)
41 s += ts;
42
43 for (unsigned i = 0; i <= s.size() - a; i++)
44 {
45 int t = binaryStr2num(s, i, i + a - 1);
46 for (int j = a; j <= b; j++)
47 {
48 if (i + j - 1 >= s.size())
49 continue;
50 t = t * 2 + (s[i + j - 1] - '0');
51 cnt[j][t]++;
52 }
53 }
54
55 while (n--)
56 {
57 int Max = 0, len, num;
58 for (int i = a; i <= b; i++)
59 for (int j = 0; j < 4096; j++)
60 if (Max < cnt[i][j])
61 Max = cnt[i][j], len = i, num = j;
62
63 if (Max == 0)
64 break;
65
66 queue <string> q;
67 for (int i = a; i <= b; i++)
68 for (int j = 0; j < 4096; j++)
69 if (cnt[i][j] == Max)
70 {
71 q.push(num2binaryStr(i, j));
72 cnt[i][j] = 0;
73 }
74
75 cout << Max << endl;
76
77 int i = 0;
78 while (q.empty() == false)
79 {
80 cout << q.front();
81
82 i++;
83 if (i % 6 == 0)
84 cout << endl;
85 else
86 cout << (q.size() == 1 ? '\n' : ' ');
87
88 q.pop();
89 }
90 }
91
92 return 0;
93 }
94