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
|
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int solution(string &ss);
int calcdif(string, string);
int toDay(string);
int main()
{
string week = "Mon 01:00-23:00\nTue 01:00-23:00\nWed 01:00-23:00\nThu 01:00-23:00\nFri 01:00-23:00\nSat 01:00-23:00\nSun 01:00-21:00";
cout << solution(week);
return 0;
}
enum Day {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
int solution(string &ss)
{
string s = ss; //reading the content of the reference
istringstream f(s);
string m ;
string p_end = "Mon 00:00"; //previous meeting end
string m_start;
string m_end;
int diff;
int max_diff;
//"Sun 15:30-20:15" //format
while (getline(f,m))
{
m_start = m.substr(0,9);
m_end = m.substr(0,3) + m.substr(10,5); //"Sun 20:15"
cout << "this is m substring 11,5 " << m.substr(10,5)<< endl; //changed from (11,5)
cout << "this is m start " << m_start << endl;
cout << "this is m end " << m_end <<endl << endl;
diff = calcdif(p_end, m_start);
if (diff>max_diff) max_diff = diff;
p_end = m_end;
}
//time between last meeting and Sun 23:59
diff = calcdif(m_start,"Sun 23:59");
if (diff>max_diff) max_diff = diff;
return max_diff;
}
//"Mon 01:00"
int calcdif(string s1, string s2) //s2-s1
{
int d1 = toDay(s1.substr(0,3));
int d2 = toDay(s2.substr(0,3));
int diff_days = d2-d1;
istringstream convert(s1.substr(5,2));
int h1;
convert>>h1;
istringstream convert2(s2.substr(5,2));
int h2 ;
convert2>>h2;
istringstream convert3(s1.substr(6,2));
int m1 ;
convert2>>m1;
istringstream convert4(s2.substr(6,2));
int m2 ;
convert2>>m2;
int tot1 = d1*60*24+h1*60+m1; //difference to start of the week
int tot2 = d2*60*24+h2*60+m2; //difference to start of the week
return tot2-tot1;
//21:25 - 19:17
//02:08 -> 128 mins
//21*60+25 - 19*60+17 = 60(21-19) + (25-17) = 60*(2) + 8 = 12
}
int toDay(string s)
{
if(s=="Mon")return 0;
if(s=="Tue")return 1;
if(s=="Wed")return 2;
if(s=="Thu")return 3;
if(s=="Fri")return 4;
if(s=="Sat")return 5;
if(s=="Sun")return 6;
return -1;
}
|