spoj 10649 dp 数位分析 未解决

 help 那位大神帮我看看到底哪儿错了

#include<iostream>
#include<cstring>
#include <cstdio>
#include<string>
#include<queue>
#include<vector> 
#include<map>
#include <set>
#include<ctime>
#include<cmath>
#include <cstdlib>
#include<algorithm>
#include <iomanip>
#include <sstream>
using namespace std;
#define LL long long 
const LL N=45;
string str1,str2;
LL dp[N][5];

string get(string str){
    if(!str.empty()) str.erase(str.end()-1);
    if(!str.empty()) str.erase(str.begin());
    return str;
}
string add(string s){
    for(LL i=s.length()-1;i>=0;i--){
        if(s[i]>'0') {
            s[i]--;
            break;
        }else s[i]='9';
    }
    return s;
}
int cmp(string str){
    while(str.length()>1){
        if(str[0]=='0') str.erase(str.begin());
        else break;
    }
    return str.compare("0");
}
LL ok(string str){
    if(str.empty()) return 1;
    string s=get(str); 
    LL ans=0;
    LL x=str[0]-'0',y=str[str.length()-1]-'0';
    if(str.length()>=2){
        if(x>8){
            ans+=dp[ str.length() ][0]+dp[ str.length() ][1]+dp[ str.length() ][2];
        }else if(x==8){
            ans+=dp[ str.length() ][0]+dp[ str.length() ][1];
            if(y>=8) ans+=ok(s);
            else {
                if(cmp(s)>0){
                    s=add(s);
                    ans+=ok(s);
                }
            }
        }else if(x>1){
            ans+=dp[ str.length() ][0]+dp[ str.length() ][1];
        }else if(x==1){
            ans+=dp[ str.length() ][0];
            if(y>=1) ans+=ok(s);
            else {
                if(cmp(s)>0){
                    s=add(s);
                    ans+=ok(s);
                }
            }
        }else if(x==0){
            ans+=ok(s);
        }
    }else {
        if(x>=8) return 3;
        else if(x>=1) return 2;
        else if(x>=0) return 1;
    }
    return ans;
}
LL solve(string str){
    string s=get(str); 
    LL ans=0;
    for(LL i=1;i<str.length();i++) for(LL j=0;j<3;j++){
        if(i==1 && j==0 ) ans+=dp[i][j];
        else if(j) ans+=dp[i][j];
    }
    LL x=str[0]-'0',y=str[str.length()-1]-'0';
    if(str.length()>=2){
        if(x>8){
            ans+=dp[ str.length() ][1]+dp[ str.length() ][2];
        }else if(x==8){
            ans+=dp[ str.length() ][1];
            if(y>=8) ans+=ok(s);
            else {
                if(cmp(s)>0){
                    s=add(s);
                    ans+=ok(s);
                }
            }
        }else if(x>1){
            ans+=dp[ str.length() ][1];
        }else if(x==1){
            if(y>=1) ans+=ok(s);
            else {
                if(cmp(s)>0){
                    s=add(s);
                    ans+=ok(s);
                }
            }
        }
    }else {
        if(x>=8) return 3;
        else if(x>=1) return 2;
        else if(x>=0) return 1;
    }
    return ans;
}
int main(){
    //freopen("a.out","w",stdout);
    dp[1][0]=1; dp[1][1]=1; dp[1][2]=1;
    dp[2][0]=1; dp[2][1]=1; dp[2][2]=1;
    for(LL i=3;i<N;i++){
        for(LL j=0;j<3;j++){
            for(LL k=0;k<3;k++){
                dp[i][j]+=dp[i-2][k];
            }
        }
    }

    LL T;
    while(cin>>T){
        while(T--){
            cin>>str1>>str2;
            while(str1.length()>1){
                if(str1[0]=='0') str1.erase(str1.begin());
                else break;
            }
            while(str2.length()>1){
                if(str2[0]=='0') str2.erase(str2.begin());
                else break;
            }
            LL flag=1;
            for(LL i=0;i<str1.length();i++){
                if(str1[i]!='0'&&str1[i]!='1'&&str1[i]!='8')
                    flag=0; break;
            }
            for(LL i=0;i<str1.length();i++){
                if(str1[i]!=str1[str1.length()-1-i]){
                    flag=0; break;
                }
            }
            cout<<solve(str2)-solve(str1)+flag<<endl;
        }
    }
}
posted @ 2012-05-06 10:51  HaoHua_Lee  阅读(210)  评论(3编辑  收藏  举报