10132 - File Fragmentation 文件碎片

 1 #include <iostream>  
2 #include <cstdio>
3 #include <string>
4 #include <cstring>
5 #include <vector>
6 #include <map>
7 using namespace std;
8 int main() {
9 int n;
10 map<string, int> m;
11 vector<string> sv;
12 scanf("%d\n\n",&n);
13 while(n--){
14 sv.clear();
15 m.clear();
16 string s;
17 int sum = 0;
18 while(getline(cin,s),s.size()>0){
19 sv.push_back(s);
20 sum += s.size();
21 }
22 int len = sum / (sv.size()/2);
23 for(size_t i = 0 ; i < sv.size()-1; i++)
24 for(size_t j = i+1; j < sv.size(); j++)
25 if(sv[i].size() + sv[j].size() == len){
26 string n = sv[i] + sv[j];
27 if(m.count(n)>0) m[n] ++; else m[n] = 1;
28 n = sv[j] + sv[i];
29 if(m.count(n)>0) m[n] ++; else m[n] = 1;
30 }
31
32 string file ;
33 int max=-1;
34 map<string,int>::iterator it;
35 for ( it=m.begin() ; it != m.end(); it++ )
36 if( (*it).second > max ){
37 file = (*it).first;
38 max = (*it).second;
39 }
40
41 cout << file << endl;
42 if(n!=0)cout << endl;
43 }
44 return 0;
45 }

10132 - File Fragmentation 文件碎片

解题报告: 根据输入,先算出结果的长度len, 然后 组合任何两个碎片能使结果等于len的,放入map中。最后遍历map,输出value最大的值!

posted @ 2012-04-08 15:59  timest  阅读(387)  评论(0编辑  收藏  举报