forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 10132 - File Fragmentation.cpp
51 lines (48 loc) · 1.27 KB
/
UVa 10132 - File Fragmentation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s;
getline(cin, s);
size_t T;
istringstream ss(s);
ss >> T;
// Skip the first empty line.
getline(cin, s);
while ( T-- )
{
vector<string> fragments;
while (getline(cin, s) && !s.empty())
{
ss.clear();
ss.str(s);
string fragment;
ss >> fragment;
fragments.push_back(fragment);
}
// Consider all concatenations of any two strings.
map<string, int> memo;
for (size_t i = 0; i < fragments.size(); ++i)
for (size_t j = i + 1; j < fragments.size(); ++j)
{
++memo[fragments[i] + fragments[j]];
++memo[fragments[j] + fragments[i]];
}
// Search for the string of highest count.
map<string, int>::iterator iter(memo.begin());
map<string, int>::iterator file(memo.begin());
for (; iter != memo.end(); ++iter)
{
if (iter->second > file->second)
file = iter;
}
cout << file->first << endl;
if (T)
cout << endl;
}
return 0;
}