dp hashing strings *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int M=1e9+7;
const int N=5000;
int n;
string s;
int dp[N+2][N+2];
int pre[N+2][N+2];
int lcp[N+2][N+2];
int main()
{
    cin>>n>>s;
    for(int i=n-1;i>=0;i--){
        for(int j=i;j>=0;j--){
            if(s[i]==s[j])
                lcp[i][j]=lcp[i+1][j+1]+1;
            lcp[j][i]=lcp[i][j];
        }
    }
    dp[0][0]=0;
    for(int i=0;i<=n;i++){
        pre[0][i]=1;
    }
    for(int i=1;i<=n;i++){
        for(int len=1;len<=i;len++){
            if(s[i-len]!='0'){
            dp[i][len]=pre[i-len][len-1];
            int p2=i-len;
            int p1=i-2*len;
            if(p1<0||p2<0)
                continue;
            int x=lcp[p1][p2];
            if(x>=len)
                continue;
            if(s[p1+x]<s[p2+x])
                dp[i][len]=(dp[i][len]+dp[i-len][len])%M;
            }
        }
        for(int j=1;j<=n;j++){
            pre[i][j]=(pre[i][j-1]+dp[i][j])%M;
        }
    }
    cout<<pre[n][n]<<endl;
}


Comments

Submit
0 Comments
More Questions

190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND