include iostream include fstream include map include set using namespa

 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <map>
#include <set>
using namespace std;
int get_length(int from, int to)
{
set<char> chars;
ifstream file;
file.open("file.txt");
file.seekg(from);
char c;
while (file >> c)
{
if (c==' ' || c=='\n') continue;
chars.insert(c);
from++;
}
return chars.size();
}
int main()
{
ifstream file;
file.open("file.txt");
char c;
map<pair<int, string>, int> words;
int pos = 0;
string word;
while (file.get(c))
{
if (c==' ' || c == '\n')
{
if (word.size())
{
words[make_pair<int, string>(pos-word.size(), word)] = pos - 1;
word.clear();
}
}
else
{
word += c;
}
pos++;
}
map<pair<int, string>, int>::iterator it, it1, it2;
cout << "Got words (word|position):" << endl;
/*for (it = words.begin(); it!=words.end(); it++)
{
cout << (*it).first.second << " " << (*it).second << endl;
}*/
map<pair<string, string>, int> lengths;
for (it1 = words.begin(); it1!=words.end(); it1++)
for (it2 = words.begin(); it2!=words.end(); it2++)
{
if (it1 == it2) continue;
if ((*it1).first.first > (*it2).first.first) continue;
lengths[make_pair<string,string>((*it1).first.second, (*it2).first.second)] = get_length((*it1).second, (*it2).first.first);
}
map<pair<string, string>, int>::iterator r;
int maxlen = 0;
for(r = lengths.begin(); r!=lengths.end(); r++)
{
if ((*r).second > maxlen) maxlen = (*r).second;
cout << (*r).first.first << " " << (*r).first.second << " " << (*r).second << endl;
}
set<string> result;
for(r = lengths.begin(); r!=lengths.end(); r++)
{
if ((*r).second == maxlen)
{
result.insert((*r).first.first);
result.insert((*r).first.second);
}
}
set<string>::iterator resiter;
cout << "Resulting set:" << endl;
for (resiter = result.begin(); resiter != result.end(); resiter++)
{
cout << (*resiter) << " ";
}
}