-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.cpp
executable file
·89 lines (80 loc) · 2.21 KB
/
contact.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
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
86
87
88
89
/*
ID: paulius10
PROG: contact
LANG: C++
*/
#include <algorithm>
#include <fstream>
#include <map>
#include <vector>
#define PATTERNS_PER_LINE 6
using namespace std;
struct pattern {
int frequency;
string pattern;
};
bool comparePatterns(const pattern& lhs, const pattern& rhs) {
if (lhs.frequency != rhs.frequency) return lhs.frequency > rhs.frequency;
int l1 = lhs.pattern.length();
int l2 = rhs.pattern.length();
if (l1 != l2) return l1 < l2;
return lhs.pattern < rhs.pattern;
}
int main() {
int A, B, N;
ifstream fin("contact.in");
fin >> A >> B >> N;
string seq = "";
while (!fin.eof()) {
string line;
fin >> line;
seq += line;
}
fin.close();
// counting
map<string, int> observations;
// for each starting index position
for (int i = 0; i <= seq.length() - A; i++) {
// for each valid pattern starting with that index
for (int len = A; len <= B && i + len <= seq.length(); len++) {
// mark that pattern as observed
string pattern = seq.substr(i, len);
map<string, int>::iterator it = observations.find(pattern);
if (it != observations.end()) it->second = it->second + 1;
else observations[pattern] = 1;
}
}
// sorting
vector<pattern> patterns;
for (map<string, int>::iterator it = observations.begin();
it != observations.end(); it++) {
pattern p;
p.frequency = it->second;
p.pattern = it->first;
patterns.push_back(p);
}
sort(patterns.begin(), patterns.end(), &comparePatterns);
ofstream fout("contact.out");
int previousFrequency = 0;
int frequencyCounter = 0;
int patternCounter = 0;
for (int i = 0; i < patterns.size(); i++) {
bool newLine = false;
if (patterns[i].frequency < previousFrequency || previousFrequency == 0) {
frequencyCounter++;
if (frequencyCounter > N) break;
if (i != 0) fout << endl;
fout << patterns[i].frequency;
previousFrequency = patterns[i].frequency;
newLine = true;
patternCounter = 0;
}
if (patternCounter % PATTERNS_PER_LINE == 0) newLine = true;
if (newLine) fout << endl << patterns[i].pattern;
else fout << ' ' << patterns[i].pattern;
patternCounter++;
}
fout << endl;
fout.close();
return 0;
}