欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ccf 201712-3 Crontab

程序员文章站 2022-07-07 18:57:05
...

ccf 201712-3	Crontabccf 201712-3	Crontabccf 201712-3	Crontab

//ac结果只有85分。。。测试过程暂时未发现错误。。求高手指出

#include <iostream>
#include <algorithm> 
#include <string>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct crontab{
	int min;
	int hour;
	int dayofmonth;
	int month;
	int dayofweek;
	int num; 
	string command;
}cro[100000];
bool  cmp1(const crontab &x, const crontab &y)  
{  
	return x.month<y.month;//从小到大排<,若要从大到小排则>  
}
bool  cmp2(const crontab &x, const crontab &y)  
{  
	return x.dayofmonth<y.dayofmonth;//从小到大排<,若要从大到小排则>  
}
bool  cmp3(const crontab &x, const crontab &y)  
{  
	return x.hour<y.hour;//从小到大排<,若要从大到小排则>  
}
bool  cmp4(const crontab &x, const crontab &y)  
{  
	return x.min<y.min;//从小到大排<,若要从大到小排则>  
}
bool  cmp5(const crontab &x, const crontab &y)  
{  
	return x.num<y.num;//从小到大排<,若要从大到小排则>  
}    
struct time{
	int year;
	int month;
	int day;
	int hour;
	int min;
};
char weekchange(string p){
	if(p=="Sun"||p=="sun")	return '0';
	if(p=="Mon"||p=="mon")	return '1';
	if(p=="Tue"||p=="tue")	return '2';
	if(p=="Wed"||p=="wed")	return '3';
	if(p=="Thu"||p=="thu")	return '4';
	if(p=="Fri"||p=="fri")	return '5';
	if(p=="Sat"||p=="sat")	return '6';
}
string monthchange(string p){
	if(p=="Jan"||p=="jan")	return "1";
	if(p=="Feb"||p=="feb")	return "2";
	if(p=="Mar"||p=="mar")	return "3";
	if(p=="Apr"||p=="apr")	return "4";
	if(p=="May"||p=="may")	return "5";
	if(p=="Jun"||p=="jun")	return "6";
	if(p=="Jul"||p=="jul")	return "7";
	if(p=="Aug"||p=="aug")	return "8";
	if(p=="Sep"||p=="sep")	return "9";
	if(p=="Oct"||p=="oct")	return "10";
	if(p=="Nov"||p=="nov")	return "11";
	if(p=="Dec"||p=="dec")	return "12";
}
void print(int i,crontab cr){
	cout<<i;
	int b=cr.month,c=cr.dayofmonth,d=cr.hour,e=cr.min; 
	if(b>=10)	cout<<b;
	else cout<<"0"<<b;
	if(c>=10)	cout<<c;
	else cout<<"0"<<c;	
	if(d>=10)	cout<<d;
	else cout<<"0"<<d;
	if(e>=10)	cout<<e<<" ";
	else cout<<"0"<<e<<" ";	
}
void datain(int a[],string s){
	fill(a,a+61,-1);
	int p1=0,x=0;
	string str; 
	while(1){
		int pos1=s.find(',',p1);
		int pos2=s.find('-',p1);
		if(pos1==-1){
			str=s.substr(p1);
			break;
		}
		if(pos2>pos1||pos2==-1){
			for(int i=pos1-1,k=1;i>=p1;k*=10,i--){
				if(a[x]==-1)	a[x]=k*(s[i]-'0');
				else a[x]+= k*(s[i]-'0');
			}
			x++;
		}
		else{
			for(int i=pos1-1,k=1;i>pos2;k*=10,i--){
				if(a[x]==-1)	a[x]=k*(s[i]-'0');
				else a[x]+= k*(s[i]-'0');
			}
			x++;
			int count=0;
			for(int i=pos2-1,k=1;i>=p1;k*=10,i--){
				count+= k*(s[i]-'0');
			}
			int flag1=a[x-1];
			for(int i=count;i<flag1;i++){
				a[x]=i;
				x++;
			}
		}
		p1=pos1+1;
	}
	int pos1=str.find('-');
	if(pos1!=-1){
		int n=str.length();
		for(int i=n-1,k=1;i>pos1;k*=10,i--){
			if(a[x]==-1)	a[x]=k*(str[i]-'0');
			else a[x]+= k*(str[i]-'0');
		}
		x++;
		int count=0;
		for(int i=pos1-1,k=1;i>=0;k*=10,i--){
			count+= k*(str[i]-'0');
		} 
		int flag2=a[x-1];
		for(int i=count;i<flag2;i++){
			a[x]=i;
			x++;
		}
	}else {
		for(int i=str.length()-1,k=1;i>=0;k*=10,i--){
			if(a[x]==-1)	a[x]=k*(str[i]-'0');
			else a[x]+=k*(str[i]-'0');
		}
	}
}
string funcmonth(string s){
	string str=""; 
	for(int i=0;i<s.length();i++){
		if(!((s[i]>='0'&&s[i]<='9')||s[i]==','||s[i]=='-')){
			string s1=s.substr(i,3);
			str+=monthchange(s1);
			i+=2;
		} 
		else str+=s[i];
	}
	return str;
}
string funcweek(string s){
	string str="";
	for(int i=0;i<s.length();i++){
		if(!((s[i]>='0'&&s[i]<='9')||s[i]==','||s[i]=='-')){
			string s1=s.substr(i,3);
			str+=weekchange(s1);
			i+=2;
		} 
		else str+=s[i];
	}
	return str;
}
int judge1(int i,crontab cr){
	int j=cr.month;
	if((i%100!=0&&i%4==0)||i%400==0){//闰年 
		if((j==4||j==6||j==9||j==11)&&cr.dayofmonth==31)	return 0; 
		else if (j==2&&cr.dayofmonth>29)	return 0;
	} 
	else {
		if((j==4||j==6||j==9||j==11)&&cr.dayofmonth==31)	return 0; 
		else if (j==2&&cr.dayofmonth>28)	return 0;
	}
	return 1;
	
}
int judge(int i,crontab cr,time tstart,time tend){
	int funcdayofweek(int t,int t1,int t2);
	int flag1=0,flag2=0;
	if(i==tstart.year&&cr.month==tstart.month&&cr.dayofmonth==tstart.day&&cr.hour==tstart.hour&&cr.min>=tstart.min) flag1=1;
	else if(i==tstart.year&&cr.month==tstart.month&&cr.dayofmonth==tstart.day&&cr.hour>tstart.hour)flag1=1;
	else if(i==tstart.year&&cr.month==tstart.month&&cr.dayofmonth>tstart.day) flag1=1;
	else if(i==tstart.year&&cr.month>tstart.month) flag1=1;
	else if(i>tstart.year)flag1=1;
	if(i==tend.year&&cr.month==tend.month&&cr.dayofmonth==tend.day&&cr.hour==tend.hour&&cr.min<tend.min) flag2=1;
	else if(i==tend.year&&cr.month==tend.month&&cr.dayofmonth==tend.day&&cr.hour<tend.hour)flag2=1;
	else if(i==tend.year&&cr.month==tend.month&&cr.dayofmonth<tend.day) flag2=1;
	else if(i==tend.year&&cr.month<tend.month) flag2=1;
	else if(i<tend.year)flag2=1;
	if(judge1(i,cr)==1&&flag2==1&&flag1==1&&funcdayofweek(i,cr.month,cr.dayofmonth)==cr.dayofweek)	return 1;
	else return 0; 
}
int funcdayofweek(int t,int t1,int t2){//t年 t1月 t2日 
	int dayofweek=4;
	for(int i=1971;i<=t;i++){
		if((i-1)%400==0||((i-1)%100!=0&&(i-1)%4==0)) dayofweek+=2;
		else dayofweek++; 
		if(dayofweek>=7)	dayofweek-=7;
	}
	for(int i=1;i<t1;i++){
		if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)	dayofweek+=3;
		else if(i==4||i==6||i==9||i==11) dayofweek+=2;
		else if(t%400==0||(t%100!=0&&t%4==0))	dayofweek++;
		if(dayofweek>=7)	dayofweek-=7;
	}
	for(int i=1;i<t2;i++){
		dayofweek++;
		if(dayofweek>=7)	dayofweek-=7;
	}
	return dayofweek; 
}
int main(int argc, char** argv) {
//	freopen("in.txt","r",stdin);
	int n;
	cin>>n;
	string start,end;//开始时间 结束时间 
	cin>>start>>end;
	time tstart,tend;
	
	tstart.year=1000*(start[0]-'0')+100*(start[1]-'0')+10*(start[2]-'0')+(start[3]-'0'); 
	tstart.month=10*(start[4]-'0')+(start[5]-'0');
	tstart.day=10*(start[6]-'0')+(start[7]-'0');
	tstart.hour=10*(start[8]-'0')+(start[9]-'0');
	tstart.min=10*(start[10]-'0')+(start[11]-'0');

	tend.year=1000*(end[0]-'0')+100*(end[1]-'0')+10*(end[2]-'0')+(end[3]-'0'); 
	tend.month=10*(end[4]-'0')+(end[5]-'0');
	tend.day=10*(end[6]-'0')+(end[7]-'0');
	tend.hour=10*(end[8]-'0')+(end[9]-'0');
	tend.min=10*(end[10]-'0')+(end[11]-'0');

	int numcr=0; 
	for(int i=0;i<n;i++){//crontab 录入 
		crontab cr;
		int a1[61],a2[61],a3[61],a4[61],a5[61];
		string c1,c2,c3,c4,c5,c6;//min hour dayofmonth month dayofweek
		cin>>c1>>c2>>c3>>c4>>c5>>c6;

		if(c1[0]=='*')	c1="0-59"; 
		if(c2[0]=='*')	c2="0-23";
		if(c3[0]=='*')	c3="1-31";
		if(c4[0]=='*')	c4="1-12";
		if(c5[0]=='*')	c5="0-6"; 
		datain(a1,c1);
		datain(a2,c2);
		datain(a3,c3);
		c4= funcmonth(c4);
		datain(a4,c4);
		c5=funcweek(c5);
		datain(a5,c5);
		for(int j=0;a4[j]!=-1;j++){//month
			for(int k1=0;a3[k1]!=-1;k1++){//dayofmonth 
				for(int k2=0;a2[k2]!=-1;k2++){//hour
					for(int k3=0;a1[k3]!=-1;k3++){//min
						for(int k4=0;a5[k4]!=-1;k4++){//dayofweek
							crontab cr1;
							cr1.num=i;cr1.command=c6;cr1.month=a4[j];cr1.dayofmonth=a3[k1];cr1.hour=a2[k2];cr1.min=a1[k3];cr1.dayofweek=a5[k4];
							for(int f=tstart.year;f<=tend.year;f++){
								if(judge(f,cr1,tstart,tend)==1){
									cro[numcr]=cr1;
									numcr++;
									break; 
								}
							} 
						}
					}
				}
			}
		}
	}
	sort(cro,cro+numcr,cmp1); 
	for(int i=1,pos1=0,pos2=0;i<numcr;i++){
		if(cro[i].month==cro[i-1].month) pos2=i;
		else{
			sort(cro+pos1,cro+pos2+1,cmp2);
			pos1=pos2=i;
		}
		if(i==numcr-1){
			sort(cro+pos1,cro+pos2+1,cmp2);			
		}
	}
	for(int i=1,pos1=0,pos2=0;i<numcr;i++){
		if(cro[i].dayofmonth==cro[i-1].dayofmonth&&cro[i].month==cro[i-1].month) pos2=i;
		else{
			sort(cro+pos1,cro+pos2+1,cmp3);
			pos1=pos2=i;
		}
		if(i==numcr-1){
			sort(cro+pos1,cro+pos2+1,cmp3);			
		}
	} 
	for(int i=1,pos1=0,pos2=0;i<numcr;i++){
		if(cro[i].dayofmonth==cro[i-1].dayofmonth&&cro[i].month==cro[i-1].month&&cro[i].hour==cro[i-1].hour) pos2=i;
		else{
			sort(cro+pos1,cro+pos2+1,cmp4);
			pos1=pos2=i;
		}
		if(i==numcr-1){
			sort(cro+pos1,cro+pos2+1,cmp4);			
		}
	}
	for(int i=1,pos1=0,pos2=0;i<numcr;i++){
		if(cro[i].dayofmonth==cro[i-1].dayofmonth&&cro[i].month==cro[i-1].month&&cro[i].hour==cro[i-1].hour&&cro[i].min==cro[i-1].min) pos2=i;
		else{
			sort(cro+pos1,cro+pos2+1,cmp5);
			pos1=pos2=i;
		}
		if(i==numcr-1){
			sort(cro+pos1,cro+pos2+1,cmp5);			
		}
	}
	for(int i=tstart.year;i<=tend.year;i++){
		for(int j=0;j<numcr;j++){
			crontab cr=cro[j];
			if(judge(i,cr,tstart,tend)==1){
				print(i,cr);
				cout<<cr.command<<"\n";
			}
		}
	}
	return 0;
}

相关标签: ccf csp认证