-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeForces245H.cpp
65 lines (49 loc) · 1.12 KB
/
codeForces245H.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
typedef long long ll;
typedef unsigned long long llu;
#define SIZE 5001
long dp[SIZE][SIZE];
char dp2[SIZE][SIZE];
char str[SIZE];
char isPalin(int x, int y){
char res = 0;
if ( x >= y ) return 1;
if ( dp2[x][y] != -1 ) return dp2[x][y];
if ( str[x] == str[y] ) res = isPalin(x + 1, y - 1);
return dp2[x][y] = res;
}
long rec(int x, int y){
long res = 0;
if ( x > y ) return 0;
if ( x == y ) return 1;
if ( dp[x][y] != -1 ) return dp[x][y];
res += rec(x, y - 1) + rec(x + 1, y);
res -= rec(x + 1, y - 1);
if ( isPalin(x,y) ) res++;
return dp[x][y] = res;
}
int main(){
unsigned long q;
int x, y;
/*while ( scanf("%s", str) == 1 ){
memset(dp2, -1, sizeof(dp2));
memset(dp, -1, sizeof(dp));
scanf("%lu", &q);
while ( q-- ){
scanf("%d%d", &x, &y);
x--, y--;
printf("%ld\n", rec(x, y));
}
}*/
scanf("%s", str);
memset(dp2, -1, sizeof(dp2));
memset(dp, -1, sizeof(dp));
scanf("%lu", &q);
while ( q-- ){
scanf("%d%d", &x, &y);
x--, y--;
printf("%ld\n", rec(x, y));
}
return 0;
}