define DEBUG if defined DEBUG define out printf __VA_ARGS__ else defin

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//#define DEBUG
#if defined(DEBUG)
#define out(x,...) printf(x,##__VA_ARGS__);
#else
#define out(x,...)
#endif
using namespace std;
struct Entry {
int count;
string word;
vector<int> bigram;
};
struct comparer {
bool operator() (const Entry &x, const Entry &y) {
return (x.count > y.count);
}
};
void hasher(const string &str, vector<int>& hash) {
int len = str.size();
if (len > 1) {
for (int i = 0; i < len - 1; i++) {
hash.push_back(str[i]*31 + str[i + 1]);
out("%c%c ", str[i], str[i + 1]);
}
sort(hash.begin(), hash.end());
} else {
hash.push_back(str[0]*31);
out("%c ", str[0]);
}
out("\n");
}
// ищем пересечение и объединение векторов a и b
void counter(const vector<int>& a, const vector<int>& b, int &unions, int &intersections) {
#ifdef DEBUG
for (auto y : a) {
cout << y << " ";
}
cout <<endl;
for (auto x : b) {
cout << x << " ";
}
cout << endl;
#endif
unions = b.size() + a.size();
intersections = 0;
int j = 0;
int i = 0;
while(i < a.size() && j < b.size()) {
int y = a[i];
int x = b[j];
// cout << x << " " << y << endl;
if (x == y) {
i++;
j++;
intersections++;
}
if (x < y) {
j++;
}
if (y < x) {
i++;
}
}
unions -= intersections;
}
struct Maximum {
const Entry *entry;
int unions;
int intersections;
int count;
};
int main() {
multiset<Entry, comparer> dictionary;
ifstream fin("count_big.txt");
while(!fin.eof()) {
Entry tmp;
fin >> tmp.word;
fin >> tmp.count;
hasher(tmp.word, tmp.bigram);
dictionary.insert(move(tmp));
}
fin.close();
string str;
while (cin >> str) {
vector<int> hash;
hasher(str, hash);
int unions = 0;
int intersections = 0;
Maximum max;
max.entry = nullptr;
max.intersections = 0;
max.unions = 1;
max.count = 0;
for (const Entry &x : dictionary) {
// cout << str << " " << x.word << " ";
counter(x.bigram, hash, unions, intersections);
int a = max.intersections * unions;
int b = max.unions * intersections;
if ( (a < b) || (( a == b ) && ( (max.count < x.count) || ((max.count == x.count) && (lexicographical_compare(x.word.begin(), x.word.end(), max.entry->word.begin(), max.entry->word.end())))))) {
max.entry = &x;
max.intersections = intersections;
max.unions = unions;
max.count = x.count;
}
// cout << intersections << " " << unions << endl;
// if (x.word == "preparing") {
// getchar();
// getchar();
// }
}
if (max.entry) {
cout << max.entry->word << endl;
}
}
return 0;
}