为什么会超时啊?

P2010 [NOIP2016 普及组] 回文日期

wxwyx @ 2019-11-14 19:49:23

向dalao求助,为什么有两个点超时

#include<bits/stdc++.h>
using namespace std;
int date[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
string sa,sb;
int ans=0;
int sy,sm,sd;
int ey,em,ed;
int main()
{
    cin>>sa>>sb;
    for(int i=0;i<4;i++)
    {
        sy=sy*10+sa[i]-'0';
        ey=ey*10+sb[i]-'0';
    }
    for(int i=4;i<6;i++)
    {
        sm=sm*10+sa[i]-'0';
        em=em*10+sb[i]-'0';
    }
    for(int i=6;i<8;i++)
    {
        sd=sd*10+sa[i]-'0';
        ed=ed*10+sb[i]-'0';
    }
//  cout<<sy<<sm<<sd<<endl;
//  cout<<ey<<em<<ed<<endl;
    for(int year=sy;year<=ey;year++)
    {
        int start=1,end=12;
        if(year==sy) start=sm;
        if(year==ey) end=em;
        for(int month=start;month<=end;month++)
        {
            int left=1,right=date[month];
            if(year==sy&&month==sm) left=sd;
            if(month==2 && (year%400==0 || (year%4==0&&year%100!=0) ) ) right=29;
            if(year==ey&&month==em) right=ed;
            for(int day=left;day<=right;day++)
            {
                stringstream ss;
                ss<<year;
                if(month<=9) ss<<"0"<<month;
                else if(month>=10) ss<<month;
                if(day<=9) ss<<"0"<<day;
                else if(day>=10) ss<<day;
                string s1=ss.str(),s2=s1;
                int len=s1.length();
                for(int i=0;i<len/2;i++)
                    swap(s1[i],s1[len-i-1]);
                if(s1==s2)
                {
        //          cout<<s1<<endl<<s2<<endl;
        //          cout<<year<<" "<<month<<" "<<day<<endl;
        //          cout<<s1<<endl;
                    ans++;
                }
            }
        }
    }
    cout<<ans<<endl;
}

by 存在 @ 2019-11-14 19:58:56

其实只用判断年份就可以了


by TESJackeyLove @ 2019-11-17 13:57:38

string有毒stringstream贼耗时间


|