Codeforces Round #272 (Div. 1)D(字符串DP)

D. Dreamoon and Binary
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Dreamoon saw a large integer x written on the ground and wants to print its binary form out. Dreamoon has accomplished the part of turning x into its binary format. Now he is going to print it in the following manner.

He has an integer n = 0 and can only perform the following two operations in any order for unlimited times each:

  1. Print n in binary form without leading zeros, each print will append to the right of previous prints.
  2. Increase n by 1.

Let's define an ideal sequence as a sequence of operations that can successfully print binary representation of x without leading zeros and ends with a print operation (i.e. operation 1). Dreamoon wants to know how many different ideal sequences are there and the length (in operations) of the shortest ideal sequence.

The answers might be large so please print them modulo 1000000007 (109 + 7).

Let's define the string representation of an ideal sequence as a string of '1' and '2' where the i-th character in the string matches thei-th operation performed. Two ideal sequences are called different if their string representations are different.

Input

The single line of the input contains a binary integer representing x (1 ≤ x < 25000) without leading zeros.

Output

The first line of the output should contain an integer representing the number of different ideal sequences modulo 1000000007 (109 + 7).

The second line of the output contains an integer representing the minimal length of an ideal sequence modulo 1000000007 (109 + 7).

Sample test(s)
input
101
output
1
6
input
11010
output
3
5
Note

For the first sample, the shortest and the only ideal sequence is «222221» of length 6.

For the second sample, there are three ideal sequences «21211», «212222222221», «222222222222222222222222221». Among them the shortest one has length 5.


題意:RT

思路:就是求將原串分成多個數字,使得所有數字從左往右不遞減即,求方案數

            dp[i][j]表示以i到j結尾的二進制數的總方案數

            dp[i][j] + = dp[k][i-1]  (其中k到i-1的二進制數要小於或等於i到j的二進制數)

            再用一個mi[i][j]表示以i到j結尾的二進制數前面最少的二進制數的數量

            mi[i][j] = min(mi[k][i-1]+1)

            注意一下細節即可

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MOD = (int)1e9+7;
const int MAXN = 5010;
int dp[MAXN][MAXN];
int mi[MAXN][MAXN];
int lcp[MAXN][MAXN],n;
char s[MAXN];

int add(int a,int b)
{
    a+=b;
    if(a>=MOD)a-=MOD;
    return a;
}

int cmp(int a,int b,int c,int d)
{
    if(b-a!=d-c)return b-a < d-c ? -1:1;
    if(lcp[a][c]>=b-a+1)return 0;
    return s[a+lcp[a][c]]=='0' ? -1:1;
}

int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=n;i>=1;i--){
        for(int j=n;j>=1;j--){
            lcp[i][j]= (s[i]==s[j] ? lcp[i+1][j+1]+1 : 0);
        }
    }
    int ans1=1,ans2,id=1;
    for(int i=1;i<=n;i++)dp[1][i]=1;
    for(int i=2;i<=n;i++){
        if(s[i]=='0')continue;
        int minv=MAXN;
        int res=0;
        for(int j=i,k=i-1;j<=n;j++){
            while(k>=1 && (s[k]=='0' || cmp(k,i-1,i,j)<=0)){
                if(s[k]=='1')minv=min(minv,mi[k][i-1]+1),res=add(res , dp[k][i-1]);
                k--;
            }
            mi[i][j]=minv;
            dp[i][j]=res;
            if(j==n&&dp[i][j]){
                ans1=add(ans1,dp[i][j]);
                id=i;
            }
        }
    }
    if(id<=(n-13)){
        int res=0;
        for(int i=id;i<=n;i++){
            res=(res*2 + s[i]-'0')%MOD;
        }
        ans2=add(res,mi[id][n]+1);
    }
    else
    for(int i=max(1,n-12);i<=n;i++){
        if(dp[i][n]){
            int res=0;
            for(int j=i;j<=n;j++){
                res=(res<<1) + s[j]-'0';
            }
            res=res + mi[i][n]+1;
            ans2=min(ans2,res);
        }
    }
    printf("%d\n%d\n",ans1,ans2);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章