dp hashing strings *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define all(x) (x).begin(), (x).end()
const int MAXN = 5005;

int dp[MAXN][MAXN];
int is[MAXN][MAXN];

void fill()
{
	for(int i = 0; i <= 5001; i++)
	{
		for(int j = 0; j <= 5001; j++)
			is[i][j] = 1;
	}
}

int32_t main()
{
	fill();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    string str; cin >> str;
    int boyut = str.size();
    for(int i = boyut; i >= 1; i--)
    {
        for(int j = i; j <= boyut; j++)
        {
            if(i == j) continue;

            if(str[i -1] == str[j -1]) is[i][j] = is[i +1][j -1];
            else is[i][j] = 0;
        }
    }

    for(int i = boyut; i >= 1; i--)
    {
        for(int j = i; j <= boyut; j++)
        {
            if(i == j)
            {
                dp[i][j] = 1;
                continue;
            }

            dp[i][j] = is[i][j];
            dp[i][j] += dp[i +1][j] + dp[i][j -1] - dp[i +1][j -1];
        }
    }

    int test; cin >> test;
    while(test--)
    {
        int l, r; cin >> l >> r;
        cout << dp[l][r] << "\n";
    }
 
    return 0;
}


Comments

Submit
0 Comments
More Questions

1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality