CodeForces - 245H Palindromes区间dp_记忆化搜索

You’ve got a string s = s1s2… s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li… ri], which are palindromes.

String s[l… r] = slsl + 1… sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2… s|s|.

String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2… t|t| = t|t|t|t| - 1… t1.

Input
The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query.

It is guaranteed that the given string consists only of lowercase English letters.

Output
Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.

Examples
Input

caaaba
5
1 1
1 4
2 3
4 6
4 5
Output
1
7
3
4
2

题目思路很简单,在这里仅做记忆化搜索的笔记

#include
#include
#include
#define N 5050
char s[N];
int n;
int q;
int l,r;
int dp[N][N];
bool f[N][N];
int result;
bool is(int x, int y)
{
if(f[x][y]) return true;//因为初始化直接另x的值全为false

else return f[x][y]=(s[x]==s[y])&&is(x+1,y-1);//f如果未被存储,就算一下

}
int main()
{
scanf("%s",s);

    scanf("%d",&q);

    int len = strlen(s);

    memset(dp,0,sizeof(dp));

    memset(f,false,sizeof(f));

    for(int i=0; i<len; i++)//初始化

       {
            dp[i][i]= 1;

            f[i][i] = true;//记忆储存
       }

    for(int i=0; i<len-1; i++)//初始化
    {
        dp[i][i+1]=2;

        if(s[i+1]==s[i])
        {
            dp[i][i+1]++;

            f[i][i+1] = true;//记忆储存
        }
    }

    for(int i=2; i<len; i++)
    {
        for(int j=0; j+i<=len-1; j++)
        {
            int p=j+i;

            dp[j][p] = dp[j+1][p] + dp[j][p-1] - dp[j+1][p-1];//打表所有长度的回文数,速度极快,因为用的是dp直接求的最优解

            if(is(j,p)) dp[j][p]++;
        }
    }

    while(q--)
    {
        scanf("%d %d",&l,&r);
        printf("%d\n",dp[l-1][r-1]);
    }

    return 0;

}

扫描二维码关注公众号,回复: 8839744 查看本文章
发布了35 篇原创文章 · 获赞 0 · 访问量 957

猜你喜欢

转载自blog.csdn.net/ACM_Cmy/article/details/97691436