include iostream include vector include algorithm include string using

  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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int comp(pair<int,double> l, pair<int,double> r)
{
return l.second > r.second;
}
struct Date
{
int day;
int month;
int year;
double conf;
};
int cmpDate(Date l, Date r)
{
return l.conf > r.conf;
}
// corrects the year
vector<pair<int,double>> year(vector<vector<pair<int,double>> > source, int k)
{
int count = 0;
vector<pair<int,double> > firstBox;
vector<pair<int,double> > secondBox;
vector<pair<int,double> > thirdBox = source[6];
vector<pair<int,double> > fourthBox = source[7];
firstBox.push_back(source[4][1]);
firstBox.push_back(source[4][2]);
secondBox.push_back(source[5][0]);
secondBox.push_back(source[5][9]);
secondBox.push_back(source[5][1]);
vector<pair<int,double>> yearsRes;
int y = 0;
double conf = 1.0;
// overrunning each box using
for(int i = 0; i < firstBox.size(); ++i)
{
for(int j = 0; j < secondBox.size(); ++j)
{
for(int m = 0; m < 10; ++m)
{
for(int n = 0; n < 10; ++n)
{
y=firstBox[i].first*1000 + secondBox[j].first*100 +thirdBox[m].first*10 +fourthBox[n].first;
conf=firstBox[i].second*secondBox[j].second*thirdBox[m].second*fourthBox[n].second;
// Checking if this number enters the range
if(y >= 1900 && y <= 2199)
yearsRes.push_back(pair<int,double>(y,conf));
}
}
}
}
// sorting the result array and select k years with the biggest probability
sort(yearsRes.begin(), yearsRes.end(),comp);
return vector<pair<int,double> > (yearsRes.begin(), yearsRes.begin() + k);
}
// Correcting the month
vector<pair<int,double>> month(vector<vector<pair<int,double>> > source, int n)
{
pair<int,double> boxOne = source[2][1];
pair<int,double> boxZero = source[2][0];
int number;
double conf;
vector<pair<int,double>> res;
// overrunning month from 10 to 12
for(int i =0; i < 3;++i)
{
number = boxOne.first*10 + source[3][i].first;
conf = boxOne.second*source[3][i].second;
res.push_back(pair<int,double>(number,conf));
}
// overrunning month from 01 to 09
for(int i = 1; i < 10; ++i)
{
number = boxZero.first*10 + source[3][i].first;
conf = boxZero.second*source[3][i].second;
res.push_back(pair<int,double>(number,conf));
}
// sort
sort(res.begin(), res.end(), comp);
return vector<pair<int,double> > (res.begin(), res.begin() + n);
}
// Corrects the day
vector<pair<int,double> > day(vector<vector<pair<int,double>> > source, int m)
{
int number;
double conf;
vector<pair<int,double> > res;
// overrunning month from 01 to 09
for(int i = 1; i < 10; ++i)
{
number = source[0][0].first*10 + source[1][i].first;
conf = source[0][0].second*source[1][i].second;
res.push_back(pair<int,double>(number,conf));
}
// overrunning month from 10 to 19
for(int i = 0; i < 10; ++i)
{
number = source[0][1].first*10 + source[1][i].first;
conf = source[0][1].second*source[1][i].second;
res.push_back(pair<int,double>(number,conf));
}
// overrunning month from 20 to 29
for(int i = 0; i < 10; ++i)
{
number = source[0][2].first*10 + source[1][i].first;
conf = source[0][2].second*source[1][i].second;
res.push_back(pair<int,double>(number,conf));
}
// overrunning month from 30 to 31
for(int i = 0; i < 2; ++i)
{
number = source[0][3].first*10 + source[1][i].first;
conf = source[0][3].second*source[1][i].second;
res.push_back(pair<int,double>(number,conf));
}
sort(res.begin(), res.end(),comp);
return vector<pair<int,double>> (res.begin(), res.begin() + m);
}
// forms the dateString
string formDate(Date d)
{
string date = "";
if(d.day < 10)
{
date+='0';
date+= d.day +'0';
date+='.';
}
else
{
date+= d.day/10 + '0';
date+= d.day%10 + '0';
date+= ".";
}
if(d.month < 10)
{
date+='0';
date+= d.month + '0';
date+='.';
}
else
{
date+= d.month/10 + '0';
date+= d.month%10 + '0';
date+= ".";
}
date+=d.year/1000 + '0';
d.year%=1000;
date+=d.year/100 + '0';
d.year%=100;
date+=d.year/10 + '0';
date+=d.year%10 + '0';
return date;
}
// returns corrected date
string date_result(vector<vector<pair<int,double>> > source, int m, int n, int k)
{
string date = "";
vector<pair<int,double>> y = year(source,k);
vector<pair<int,double>> mm = month(source, n);
vector<pair<int,double>> d = day(source, m);
vector<Date> res;
int tempYear;
int tempMonth;
int tempDay;
double conf;
// overruns each tripples and forms the array of combinations
for(int yCount = 0; yCount < k;++yCount)
{
for(int mCount = 0; mCount <n; ++mCount)
{
for(int dCount = 0; dCount < m; ++dCount)
{
tempYear = y[yCount].first;
tempMonth = mm[mCount].first;
tempDay = d[dCount].first;
conf = y[yCount].second*mm[mCount].second*d[dCount].second;
Date date;
date.day = tempDay;
date.month = tempMonth;
date.year = tempYear;
date.conf = conf;
res.push_back(date);
}
}
}
sort(res.begin(), res.end(), cmpDate);
int daySample[] = {31,28,31,30,31,30,31,31,30,31,30,31};
//overruns the array of combinations and checks if the date is valid. If it is valid, sends it to formData() function
for(int i = 0; i < res.size(); ++i)
{
bool checkYear = (res[i].year%4 == 0 && res[i].year%100 != 0) || (res[i].year%400 == 0);
if(res[i].month == 2)
{
if(checkYear && res[i].day >= 1 && res[i].day <= 29)
{
date = formDate(res[i]);
break;
}
else if(!checkYear && res[i].day >= 1 && res[i].day <= 28)
{
date = formDate(res[i]);
break;
}
}
else if(res[i].day >= 1 && res[i].day <= daySample[res[i].month-1])
{
date = formDate(res[i]);
break;
}
}
return date;
}
int main()
{
vector<vector<pair<int,double> > > source(8);
int length = 8, num = 10;
vector<pair<int,double>> temp(num);
double ver;
for(int i = 0; i < length; ++i)
{
for(int j = 0; j < num; ++j)
{
cin >> ver;
temp[j] = pair<int,double> (j, ver);
}
source[i] = temp;
}
int n = 2, m = 3, k = 3;
cout << date_result(source,n,m,k) << endl;
}