USACO 3.1 Contact

1 /*
2 PROG: contact
3 ID: jiafeim1
4 LANG: C++
5  */
6 #include <algorithm>
7 #include <iostream>
8 #include <fstream>
9 #include <string>
10 #include <map>
11  using namespace std;
12 string::size_type a,b;
13 int n;
14 map<string,long> res;
15
16 struct ar
17 {
18 string me;
19 int amount;
20 string::size_type size;
21 bool operator <(const ar& ir) const
22 {
23 if (amount!=ir.amount)
24 return ir.amount<amount;
25 if(size!=ir.size)
26 return size<ir.size;
27 return me<ir.me;
28 }
29 };
30
31 ar strs[10005];
32 int top=0;
33 int main()
34 {
35 std::ifstream fin("contact.in");
36 std::ofstream fout("contact.out");
37 fin>>a>>b>>n;
38
39 string target,temp;
40 while(fin>>temp) target.append(temp);
41
42 string::size_type i = 0,size = target.size(),num;
43 for(;i+a<=size;++i)
44 for(num=a;i+num<=size && num<=b;++num)
45 {
46 temp = target.substr(i,num);
47 if(res.count(temp)>0)
48 ++res[temp];
49 else
50 res[temp]=1;
51 }
52 for(map<string,long>::iterator iter = res.begin();
53 iter!=res.end();++iter)
54 {
55 strs[top].me=iter->first;
56 strs[top].amount=iter->second;
57 strs[top].size=strs[top].me.size();
58 ++top;
59 }
60 sort(strs,strs+top);
61
62 int cur_amount;
63 int count = 0;
64 for(int i = 0;count<n && i<top;++count)
65 {
66 cur_amount=strs[i].amount;
67 fout<<cur_amount<<endl;
68 bool fi = true;
69 int cur = 1;
70 while(cur_amount==strs[i].amount && i<top)
71 {
72 if(!fi) fout<<" ";
73 fout<<strs[i].me;
74 fi = false;
75 if(cur%6==0)
76 {
77 fout<<endl;
78 fi=true;
79 }
80 ++cur;
81 ++i;
82 }
83 --cur;
84 if(cur%6!=0)fout<<endl;
85 }
86 fin.close();
87 fout.close();
88 return 0;
89 }

TASK: contact
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3156 KB]
   Test 2: TEST OK [0.000 secs, 3156 KB]
   Test 3: TEST OK [0.000 secs, 3156 KB]
   Test 4: TEST OK [0.054 secs, 3156 KB]
   Test 5: TEST OK [0.243 secs, 3428 KB]
   Test 6: TEST OK [0.405 secs, 3172 KB]
   Test 7: TEST OK [0.378 secs, 3304 KB]

All tests OK.

posted @ 2011-05-12 22:13  幻魇  阅读(216)  评论(0编辑  收藏  举报