0w1

HR Square Subsequences ( DP + Common Subsequence )

https://www.hackerrank.com/challenges/square-subsequences
Sum up all the number of different common subsequences, for
a = s[ 0, x ), b = s[ x ] + s( x, s.size() ) Ɐ 1 ≤ x < s.size(),
with an additional restriction where s[ x ] must be taken in b.

// number of common subsequence of s[ 0, x ) and s[ x ] + s( x, s.size() )
int countCS( int x, const string &s ){
    string a = s.substr( 0, x );
    string b = s.substr( x );
    vvi dp( a.size() + 1, vi( b.size() + 1 ) );
    for( int i = 0; i < a.size(); ++i )
        dp[ i + 1 ][ 1 ] = dp[ i ][ 1 ] + ( a[ i ] == b[ 0 ] );
    for( int i = 0; i < a.size(); ++i )
        for( int j = 1; j < b.size(); ++j ){
            ( dp[ i + 1 ][ j + 1 ] = dp[ i + 1 ][ j ] + dp[ i ][ j + 1 ] - dp[ i ][ j ] ) %= MOD7;
            if( a[ i ] == b[ j ] ) ( dp[ i + 1 ][ j + 1 ] += dp[ i ][ j ] ) %= MOD7;
        }
    return dp[ a.size() ][ b.size() ];
}

void solve(){
    string s; cin >> s;
    int ans = 0;
    for( int i = 1; i < s.size(); ++i )
        ( ans += countCS( i, s ) ) %= MOD7;
    cout << ans << "\n";
}