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.

转载于:https://www.cnblogs.com/huanyan/archive/2011/05/12/USACO_Contact.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值