201712-3 crontab 6s

这种恶心的大模拟一定要提前规划好思路再动手。
string类的find、substr这种基本的不会还是很痛苦的。
看到有人用set存时间,但是感觉取得时候可能慢一点…反正空间够用,统一用个int[60]不是很好~?
一开始只有70分,后来发现时间区间是左闭右开,就过了…..一下子扣30分真狠啊….
文本暴力…看到有人说用找出命令匹配的时间进行排序,确实很妙,但是已经没有体力和心情再做这题了 :( 886

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<limits.h>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#define iter(i,start,end) for(int (i)=(start);(i)<(end);(i)++)
using namespace std;

int monthday[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};

struct command
{
    bool info[5][60];      //0:m 1:h 2:dd 3:mm 4:d   最大的范围<60
    string name;
    // set<int> info[5];   
};
int n;  long long s,t;  //256MB  10s
command coms[20];   //最多有20个命令
// int year[2],m[2],h[2],dd[2],mm[2],d[2];
int year[2];
int time[5][2]; //0 是m 1是h 2是dd 3是mm 4是d

void init();
void solve();
void jiexi(long long x,int status);
int main()
{
    // freopen("201712-3.txt","r",stdin);
    while(scanf("%d %lld %lld",&n,&s,&t)==3)
    {
        init();
        // cout<<s<<"\t"<<t<<endl;
        jiexi(s,0); jiexi(t,1);
        //check  ok
             // cout<<year[0]<<"\t"<<time[3][0]<<"\t"<<time[2][0]<<"\t"<<time[1][0]<<"\t"<<time[0][0]<<"\t"<<time[4][0]<<endl;
        solve();
    }
    return 0;
}
bool rn(int i){
    return (i%4 == 0 && i%100 != 0) || i %400 == 0;
}

long long curtime()
{
    return year[0]*pow(10,8)+time[3][0]*pow(10,6)+time[2][0]*pow(10,4)+time[1][0]*pow(10,2)+time[0][0];
}

void solve()
{
    bool run=rn(year[0]);
    while(true)
    {
        if(time[0][0]>=60){time[0][0]=0;    time[1][0]++;       //mm->hh
            if(time[1][0]>=24){time[1][0]=0;  time[2][0]++; time[4][0]=(time[4][0]+1)%7;    //hh->dd,d
                if(time[2][0]>monthday[run][time[3][0]]){time[2][0]=1;   time[3][0]++;                  //dd ->mm
                    if(time[3][0]>12){   time[3][0]=1;          //mm-> year
                        year[0]++;  run=rn(year[0]);
                    }
                }
            }
        }
        if(year[0]==year[1] && time[0][0]==time[0][1] && time[1][0]==time[1][1] && time[2][0]==time[2][1] && time[3][0]==time[3][1])    break; 
        iter(i,0,n)
        {
            int j=0;
            // iter(j,0,5)
            for(;j<5;j++)
            {
                if(!coms[i].info[j][time[j][0]]) break;
            }
            if(j==5) cout<<curtime()<<" "<<coms[i].name<<endl;
        }

        //cout<<"当前year:"<<year[0]<<"\tmonth: "<<time[3][0]<<"\tday:"<<time[2][0]<<"\thour: "<<time[1][0]<<"\tminute: "<<time[0][0]<<endl;
        time[0][0]++;   
    }
}
void jiexi(long long  x,int status)
{
    year[status]=x/pow(10,8);  x=x%(int)pow(10,8);
    time[3][status]=x/pow(10,6);    x=x%(int)pow(10,6);
    time[2][status]=x/pow(10,4);     x=x%(int)pow(10,4);
    time[1][status]=x/pow(10,2);      x=x%(int)pow(10,2);
    time[0][status]=x;
    int days=0;
    iter(i,1970,year[status])
    {   
        if(rn(i)) days+=366;  //闰年
        else days+=365;
    }
    bool run=false;
    if(rn(year[status])) run=true;
    iter(i,1,time[3][status])
        days+=monthday[run][i];
    days+=time[2][status];
    time[4][status]=(days%7+3)%7;
}


void pushall(int i,int j){  //命令i 信息j
    int lb=0,ub=0; 
    switch(j)                   //感觉这边不这样习额也可以....
    {
        case 0:
            ub=60;break;
        case 1:
            ub=24;break;
        case 2:
            lb=1;   ub=31+1;  break;          //【这里如果是按给的时间匹配 应该问题不大的 也应该这样】
        case 3:
            lb=1;   ub=12+1;  break;
        case 4:
            ub=7 +1;   break;
    };
    iter(k,lb,ub)
        coms[i].info[j][k]=true;

            // cout<<"\t全被置1\n";
}

int fanyi(string x)
{
    if(isdigit(x[0]))
        return atoi(x.c_str());
    else
    {
        iter(i, 0, x.size())
            x[i] = tolower(x[i]);   
        // cout<<"转换后的"<<x<<endl;
        //x=strlwr(x.c_str())  ;    
        if(x=="sun" )return 0;  if(x=="jan" ||x=="mon")return 1;
        if(x=="feb" ||x=="tue")return 2;  if(x=="mar" ||x=="wed")return 3;
        if(x=="apr" ||x=="thu")return 4;  if(x=="may" ||x=="fri")return 5;
        if(x=="jun" ||x=="sat")return 6;  
        if(x=="jul" )return 7;  if(x=="aug" )return 8;
        if(x=="sep" )return 9;  if(x=="oct" )return 10;
        if(x=="nov" )return 11;  if(x=="dec")return 12;
        if(x=="jan" )return 0;  if(x=="jan" )return 0;
        if(x=="jan" )return 0;  if(x=="jan" )return 0;

            fprintf(stderr,"error string");return 0;
    }

}

void init(){
    string dummy;
    iter(i,0,n){ //第i个命令
        iter(j,0,5){    //第j个信息
            cin>>dummy; 
                // cout<<"当前读入信息"<<i<<"第"<<j<<"个信息 :"<<dummy<<endl;
            if(dummy=="*")  {pushall(i,j);  continue;}     //*号
            // if(isdigit(dummy[0])){
                int pos=0,  comma=0 , dash=0   ;   //当前要解析的子字符串的起始位置,comma的位置 ,dash的位置
                bool con=true;
                string tmp=dummy;
                while(con){   //只要子字符串非空就继续解析
                    if((comma=dummy.find(',',pos))==string::npos) 
                    {
                        con=false;    // , 是否不是最后一个字串 并继续
                        tmp=dummy.substr(pos);
                    }
                    else  tmp = dummy.substr(pos, comma - pos);  

                    if ((dash = tmp.find('-')) != string::npos)    // -    如果有破折号的话
                    {
                            int start=fanyi(tmp.substr(0, dash));
                            int end=fanyi(tmp.substr(dash + 1).c_str());
                         //check
                                    // cout<<'\t'<<start<<"到"<<end<<"全被置1\n";
                            while(start<=end)
                                coms[i].info[j][start++]=true;        

                    }
                    else{    //如果没有破折号只有一个数字的话
                        int start=fanyi(tmp);
                        coms[i].info[j][start]=true;

                                // cout<<'\t'<<start<<"被置1\n";
                    }
                    if (con)    pos = comma + 1;
                }
            // }
        }
        cin>>coms[i].name;
    }

}

“`

猜你喜欢

转载自blog.csdn.net/Hesy_H/article/details/82466962