題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=3886
題意:這題的題意有點晦澀難懂,大概意思就是給你一個區間,讓你找一些滿足遞增遞減條件的數,舉個列:/-\,要匹配這個關係,把一個數字分成一列數位,滿足先遞增,然後相等,然後遞減的關係:ie:123321,1221,123441,這些都滿足/-\。
題解:設dp[i][j][k]表示考慮到第i位,上一個數爲j,匹配關係到了k,然後DP下去就行了,注意處理左區間-1和前導零
#include<cstdio>
#include<cstring>
#include<cmath>
#define F(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
char ops[111],a[111],b[111];
int dig[111],st,len,oplen;
LL N=(LL)1e10,dp[111][11][111];
int check(int pre,int now,int idx){
char c=ops[idx];
if(c=='/')return pre<now;
else if(c=='-')return pre==now;
return pre>now;
}
LL dfs(int pos,int idx=0,int pre=0,int z=0,bool inf=1){
if(!pos)return idx==oplen;
if(!inf&&~dp[pos][pre][idx])return dp[pos][pre][idx];
int end=inf?dig[pos]:9;LL ans=0;
F(i,0,end){
if(!z)ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
else if(idx<oplen&&check(pre,i,idx))
ans+=dfs(pos-1,idx+1,i,z||i,inf&&i==end),ans%=N;
else if(idx>0&&check(pre,i,idx-1))
ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
}
if(!inf)dp[pos][pre][idx]=ans;
return ans;
}
void f_ck(){
memset(dp,-1,sizeof(dp));
oplen=strlen(ops);
for(st=0,len=0;a[st]=='0';)st++;
int end=strlen(a);
for(int i=end-1;i>=st;i--)dig[++len]=a[i]-'0';
dig[1]--;//處理左區間-1
for(int i=1;dig[i]<0;)dig[i]=9,dig[i+1]--;
LL tmp=(len==0)?0:dfs(len);
for(st=0,len=0;b[st]=='0';)st++;
end=strlen(b);
for(int i=end-1;i>=st;i--)dig[++len]=b[i]-'0';
LL an=dfs(len)-tmp+N;
printf("%08lld\n",an%(LL)1e8);
}
int main(){
while(~scanf("%s%s%s",ops,a,b))f_ck();
return 0;
}