问题描述
试题编号: | 201712-3 |
试题名称: | Crontab |
时间限制: | 10.0s |
内存限制: | 256.0MB |
问题描述: |
样例输入 3 201711170032 201711222352 0 7 * * 1,3-5 get_up 30 23 * * Sat,Sun go_to_bed 15 12,18 * * * have_dinner 样例输出 201711170700 get_up 201711171215 have_dinner 201711171815 have_dinner 201711181215 have_dinner 201711181815 have_dinner 201711182330 go_to_bed 201711191215 have_dinner 201711191815 have_dinner 201711192330 go_to_bed 201711200700 get_up 201711201215 have_dinner 201711201815 have_dinner 201711211215 have_dinner 201711211815 have_dinner 201711220700 get_up 201711221215 have_dinner 201711221815 have_dinner |
1 #include<bits/stdc++.h> 2 #define pb emplace_back 3 using namespace std; 4 typedef vector<string> vecstr; 5 const char Month[14][4]={"","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"}; 6 const char Week[8][4]={"sun","mon","tue","wed","thu","fri","sat"}; 7 int n,cnt,Daysofmon[14]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 8 map<string,int> weekmap,monthmap; map<string,vecstr > all; 9 string stime,etime; 10 inline void stdit(string &s){transform(s.begin(),s.end(),s.begin(),::tolower);} 11 inline string i2s(int a){ 12 ostringstream o;o<<a; 13 return o.str(); 14 } 15 inline int s2i(const string &s){ 16 int num; 17 istringstream i(s);i>>num; 18 return num; 19 } 20 inline bool leap(int year){ 21 return (year%400==0)||(year%100!=0&&year%4==0); 22 } 23 string getweek(const string &cyyyy,const string &cmm,const string &cdd){ 24 int year=s2i(cyyyy),mm=s2i(cmm),dd=s2i(cdd); 25 int sum=0; 26 for(int i=1970;i<year;i++) sum+=leap(i)?366:365; 27 Daysofmon[2]=leap(year)?29:28; 28 for(int i=1;i<mm;i++) sum+=Daysofmon[i]; 29 sum+=dd-1; 30 return i2s((4+sum)%7); 31 } 32 vecstr setvec(string &str,int type=0){ 33 vecstr rev; 34 str+=','; 35 string curs,arry[2];int brd[2]; 36 for(int ix,fx;~(ix=str.find(','));){ 37 curs=str.substr(0,ix); 38 str=str.substr(ix+1); 39 fx=curs.find('-'); 40 if(~fx){ 41 arry[0]=curs.substr(0,fx); 42 arry[1]=curs.substr(fx+1); 43 for(int b=0;b<2;b++){ 44 string &ss=arry[b]; 45 if(isalpha(ss[0])){ 46 brd[b]=(type==1)?weekmap[ss]:monthmap[ss]; 47 } 48 else{ 49 brd[b]=s2i(ss); 50 } 51 } 52 for(int i=brd[0];i<=brd[1];i++) rev.pb(i2s(i)); 53 } 54 else{ 55 if(isalpha(curs[0])){ 56 if(type==1) rev.pb(i2s(weekmap[curs])); 57 else rev.pb(i2s(monthmap[curs])); 58 } 59 else{ 60 rev.pb(curs); 61 } 62 } 63 } 64 return rev; 65 } 66 int main(){ 67 cin>>n; 68 cin>>stime>>etime; 69 int syear=s2i(stime.substr(0,4)),eyear=s2i(etime.substr(0,4)); 70 for(int i=0;i<7;i++) weekmap[Week[i]]=i; 71 for(int i=1;i<=12;i++) monthmap[Month[i]]=i; 72 for(string minutes,hours,d_of_m,month,d_of_w,command;n--;){ 73 cin>>minutes>>hours>>d_of_m>>month>>d_of_w>>command; 74 stdit(month); 75 stdit(d_of_w); 76 if(minutes=="*") minutes="0-59"; 77 vecstr vminutes=setvec(minutes); 78 if(hours=="*") hours="0-23"; 79 vecstr vhours=setvec(hours); 80 if(d_of_m=="*") d_of_m="1-31"; 81 vecstr vd_of_m=setvec(d_of_m); 82 if(month=="*") month="1-12"; 83 vecstr vmonth=setvec(month,2); 84 if(d_of_w=="*") d_of_w="0-6"; 85 vecstr vd_of_w=setvec(d_of_w,1); 86 set<string> one(vd_of_w.begin(),vd_of_w.end()); 87 for(int nowyear=syear;nowyear<=eyear;nowyear++){ 88 Daysofmon[2]=leap(nowyear)?29:28; 89 for(auto &i:vmonth){ 90 for(auto &j:vd_of_m){ 91 string cyear=i2s(nowyear); 92 if(Daysofmon[s2i(i)]<s2i(j)||one.count(getweek(cyear,i,j))==0) continue; 93 for(auto &k:vhours){ 94 for(auto &m:vminutes){ 95 string ss=cyear 96 +(i.size()>1?i:"0"+i) 97 +(j.size()>1?j:"0"+j) 98 +(k.size()>1?k:"0"+k) 99 +(m.size()>1?m:"0"+m); 100 if(ss>=stime&&ss<etime) all[ss].pb(command); 101 } 102 } 103 } 104 } 105 } 106 } 107 for(auto &i:all){ 108 set<string> haxi; 109 for(auto &k:i.second){ 110 if(haxi.count(k)) continue; 111 cout<<i.first<<" "<<k<<"\n"; 112 haxi.insert(k); 113 } 114 } 115 return 0; 116 }