1 条题解
-
0xuyifeng LV 10 MOD @ 2017-08-23 14:27:06
---------------------------------------------AC code---------------------------------------------
#include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int MAXLEN = 5005; char s[MAXLEN]; int t, a, b, len; LL g[MAXLEN][MAXLEN]; void init(){ for(int i = 1; i <= len; i++){ int l = i, r = i; while(l >= 1 && r <= len){ if(s[l] == s[r]) g[l--][r++] = 1; else break; } l = i-1, r = i; while(l >= 1 && r <= len){ if(s[l] == s[r]) g[l--][r++] = 1; else break; } } for(int i = 1; i <= len; i++) for(int j = 1; j <= len; j++) g[i][j] += g[i-1][j] + g[i][j-1] - g[i-1][j-1]; } int main(){ scanf("%s", s+1); len = strlen(s+1); scanf("%d", &t); init(); while(t--){ scanf("%d%d", &a, &b); printf("%d\n", g[b][b] - g[a-1][b] - g[b][a-1] + g[a-1][a-1]); } return 0; }
- 1