cf 158div.2 B. Ancient Prophesy

简介:

  要求找到严格符合的dd-mm-yyyy的出现次数最多的日期,是严格符合,多个-或少个0都不可以,所以这题直接匹配就可以了

  比赛时候一直写的是分割,WA了n次……

 

这题主要学到了stringstream和strtok的用法。

strtok(s,"-");

strtok(NULL,"-"); 

只是对原字符串进行分割,并没有复制给一个新的,返回的指针是原字符串的位置。

直接匹配:

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <map>
#define INF 1E9
using namespace std;
char s[100010];
const int D[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
map<string,int> num;
int main()
{
    gets(s);
    int i,day,month,year,Max=0;
    stringstream st;
    string t,ans;
    for(i=2;s[i+7];i++)
    {
        if(s[i]!='-'||s[i+3]!='-'||s[i+1]=='-'||s[i+2]=='-')continue;
        if(s[i-1]=='-'||s[i-2]=='-'||s[i+4]=='-'||s[i+6]=='-'||s[i+7]=='-'||s[i+5]=='-')continue;
        day=s[i-1]-'0'+(s[i-2]-'0')*10;
        month=(s[i+1]-'0')*10+s[i+2]-'0';
        year=(s[i+4]-'0')*1000+(s[i+5]-'0')*100+(s[i+6]-'0')*10+s[i+7]-'0';
        if(year<2013||year>2015)continue;
        if(month<1||month>12)continue;
        if(day<1||day>D[month])continue;
        if(day<10)st<<"0";
        st<<day<<"-";
        if(month<10)st<<"0";
        st<<month<<"-"<<year;
        st>>t;
        st.clear();
        num[t]++;
        if(num[t]>Max)
        {
            Max=num[t];
            ans=t;
        }
        //cout<<t<<endl;
    }
    cout<<ans<<endl;
}


划分:

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <map>
#define INF 1E9
using namespace std;
char s[100010];
map<string,int> num;
int Max=0;
string ans;
int D[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char *p,*ll,*l,*now;
int main()
{
    gets(s);
    ll=strtok(s,"-");
    l=strtok(NULL,"-");
    bool flag=1,rflag;
    long long year,month,day;
    stringstream tt;
    string t;
    for(;now=strtok(NULL,"-");ll=l,l=now)
    {
        flag=1;
        int len=strlen(now);
        char te;
        if(len>4)
        {
            te=now[4];
            now[4]='\0';
        }
        if(len<4)continue;
        sscanf(now,"%I64d",&year);
        if(len>4) now[4]=te;
        if(*(l-1)=='-'||*(l+3)=='-')continue;
        switch(year)
        {
            case 2013:case 2014:case 2015:break;
            default:flag=0;break;
        }
        if(!flag)continue;
        if(strlen(l)!=2)continue;
        sscanf(l,"%I64d",&month);
        if(month<1||month>12)continue;
        sscanf(ll,"%I64d",&day);
        day%=100;
        if(day<1||day>D[month-1])continue;
        if(day<10)tt<<"0";
        tt<<day<<"-";
        if(month<10)tt<<"0";
        tt<<month<<"-"<<year;
        tt>>t;
        tt.clear();
        num[t]++;
        if(num[t]>Max)
        {
            Max=num[t];
            ans=t;
        }
    }
    cout<<ans<<endl;
}


 

目录
相关文章
|
1月前
|
传感器
AC31 40和50系列
AC31 40和50系列
AC31 40和50系列
|
9月前
CF484A Bits
CF484A Bits
|
人工智能
cf 220B(莫队)
cf 220B(莫队)
75 0
|
人工智能
CF628B
CF628B
47 0
|
人工智能 网络架构
CF 698A
CF 698A
58 0
CF1000F One Occurrence(莫队)
CF1000F One Occurrence(莫队)
39 0