Final Kichiku “Lanlanshu”
Problem Description
During 2010 summer training, temperlisyer often does problem like this:
“Consider a decimal integer as sequence of digits {D0, D1 … Dn-1} (D0 > 0), if exists such x, y and z, satisfying:
- Di−1<Di(0<i<=x)
- Di−1=Di(x<i<=y)
- Di−1<Di(y<i<=z)
- Di−1>Di(z<i<=n−1)
We call this integer “Lanlanshu”, now give you two numbers A and B, calculate how many “Lanlanshu” are in [A, B].“
He solved so many of these and finally get bored, and then get crazy! He decided to make up a problem to put this type of problems to an end.
Give you a string str consists only by ‘/’, ‘-‘ and ‘\’, and its length is l. Consider a decimal integer as sequence of digits {D0, D1 … Dn-1} (D0 > 0), define x0=0, xl=n-1, if exists such x1, x2…xl (x0 < x1 < x2 < … < xl) satisfying:
1. If str[i]=’/’,
Dj−1<Dj(xi<j<=xi+1)
2. If str[i]=’-’,
Dj−1=Dj(xi<j<=xi+1)
3. If str[i]=’\’,
Dj−1>Dj(xi<j<=xi+1)
We call it Final Kichiku “Lanlanshu”, now give you two numbers A and B, calculate how many Final Kichiku “Lanlanshu” are in [A, B]. This number maybe huge, we only want to now the last 8 digits of the result.
Input
Multiple cases (no more than 100), for each case:
The first line is string str, length is below 100.
The second line contains two integers A and B (0≤Ai≤Bi≤10^100).
Input terminates by EOF.
Output
For each case, output 8 digits representing the last 8 digits of the number of Final Kichiku “Lanlanshu” in [A, B]. If it’s less than 8 digits, fill it with leading zeros.
Sample Input
/\
01221 2012
Sample Output
00000315
Author
temperlsyer
Source
2011 Multi-University Training Contest 5 - Host by BNU
题意: A,B之间有多少个数,满足给定字符串的趋势
思路:dp[cur][pos][last]表示处理到当前为止,满足趋势字符串到pos位置,上一位是last的有多少个
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int MOD=1e8;
const int maxn=10;
string A,B;
string st;
int stlen,alen;
int dp[110][110][10];
int dig[110];
string subone(string A)
{
int len=A.size();
if(A[len-1]>'0')
{
A[len-1]-=1;
return A;
}
else
{
int i=len-1;
while(i>=0&&A[i]<='0')A[i]='9',i--;
A[i]-=1;
return A;
}
}
int check(int x,int y,char op)
{
if(op=='/')return x<y;
if(op=='-')return x==y;
return x>y;
}
int dfs(int cur,int e,int z,int pos,int last)
{
if(cur==alen)return pos==stlen;
if(!e&&!z&&dp[cur][pos][last]!=-1)
return dp[cur][pos][last];
int end=e?dig[cur]:9;
int ans=0;
for(int i=0;i<=end;i++)
{
if(z)ans+=dfs(cur+1,e&&i==end,z&&i==0,0,i);
else
{
if(pos<stlen&&check(last,i,st[pos]))
ans+=dfs(cur+1,e&&i==end,0,pos+1,i);
else if(pos>0&&check(last,i,st[pos-1]))
ans+=dfs(cur+1,e&&i==end,0,pos,i);
}
ans%=MOD;
}
if(!e&&!z)dp[cur][pos][last]=ans;
return ans;
}
int solve(string a)
{
memset(dp,-1,sizeof(dp));
int len=a.size();
alen=a.size();
int st=0;
while(a[st]=='0')st++;
for(int i=st;i<len;i++)dig[i]=a[i]-'0';
return dfs(st,1,1,0,0);
}
int main()
{
while(cin>>st)
{
cin>>A>>B;
stlen=st.size();
int len=A.size();
A=subone(A);
printf("%08d\n",(solve(B)-solve(A)+MOD)%MOD);
}
return 0;
}