dp --- Codeforces 245H :Queries for Number of Palindromes

Queries for Number of Palindromes

Problem‘s Link:   http://codeforces.com/problemset/problem/245/H



Mean:

给你一个字符串,然后q个询问:从i到j这段字符串中存在多少个回文串。

analyse:

dp[i][j]表示i~j这段的回文串数。

首先判断i~j是否为回文,是则dp[i][j]=1,否则dp[i][j]=0;

那么dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i+1[j]-dp[i+1][j-1],从后往前推;

注意判断dp[i][j]是否是回文也需要从后往前推,否则超时。

Time complexity: O(n*n)

Source code: 

//  Memory   Time
//  1347K     0MS
//   by : crazyacking
//   2015-03-31-16.17
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 5005
#define LL long long
using namespace std;
char str[MAXN];
int dp[MAXN][MAXN];

bool judge(int sta,int en)
{
        for(int i=sta,j=en;i<j;++i,--j)
        {
                if(str[i]!=str[j])
                        return false;
        }
        return true;
}
int main()
{
        gets(str);
        int len=strlen(str);
        memset(dp,0,sizeof dp);
        for(int i=0;i<len;++i)
        {
                if(str[i]==str[i+1])
                        dp[i][i+1]=1;
                dp[i][i]=1;
        }
        for(int i=len-1;i>=0;--i)
        {
                for(int j=i;j<len;++j)
                {
                        if(dp[i+1][j-1]==1&&str[i]==str[j])
                                dp[i][j]=1;
                }
        }
        for(int i=len-1;i>=0;--i)
        {
                for(int j=i;j<len;++j)
                {
                        dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
                }
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
                int x,y;
                scanf("%d %d",&x,&y);
                printf("%d\n",dp[x-1][y-1]);
        }
        return 0;
}

时间: 2024-08-05 21:11:17

dp --- Codeforces 245H :Queries for Number of Palindromes的相关文章

Codeforces 245H Queries for Number of Palindromes:区间dp

题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的个数. 题解: 表示状态: dp[x][y] = numbers 表示区间[x,y]中的回文子串个数. 找出答案: 每次询问:ans = dp[x][y] 如何转移: dp[x][y] = dp[x][y-1] + dp[x+1][y] - dp[x+1][y-1] + pal[x][y] 用到了容

Codeforces 245H Queries for Number of Palindromes

题意:给你一个字符串,再给你一个q(询问个数1000000),问你这个区间内回文串的个数. 解题思路: 1)dp,先把 i 到j 是回文串的真值赋值给 dp[i][j] ,然后从后往前dp    dp[i][j] += dp[i+1][j] + dp[i][j-1]  -dp[i+1][j-1]; 解题代码: 1 // File Name: 245h.dp.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月30日 星期一 15时27分10

「Codeforces」245H Queries for Number of 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

codeforces 245H H. Queries for Number of Palindromes(区间dp)

题目链接: codeforces 245H 题目大意: 给出一个字符串,询问任意区间内的回文子串的个数. 题目分析: 定义isPar[i][j]表示区间字符串[i,j]是否是回文,可以通过isPar[i+1][j-1]递推得到. 定义dp[i][j]表示及区间[i,j]内的回文子串的个数,转移方程如下: dp[i][j]=dp[i+1][j]+dp[i][j?1]?dp[i+1][j?1]+isPar[i][j] 用到了一点容斥的思想. AC代码: #include <iostream> #i

[CF245H] Queries for Number of Palindromes (容斥原理dp计数)

题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串的个数. 容斥原理: dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]; if( str[i]==str[j] 并且 str[i+1..j-1]是回文串 ) dp[i][j]++; 代码: 1 #include <cstdio> 2 #include &l

CF245H Queries for Number of Palindromes(回文树)

题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q(1<=q<=10^6) 第2至2+q行 给出每组询问的l和r 输出格式 输出每组询问所问的数量. 题目描述 You've got a string s=\(s_{1}\)\(s_{2}\)...\(s_{|s|}\) of length |s| , consisting of lowercase

HDOJ 3948 The Number of Palindromes 后缀数组

后缀数组求有多少个不同的回文串 The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1976    Accepted Submission(s): 690 Problem Description Now, you are given a string S. We want to kno

HDOJ 3948 The Number of Palindromes 回文串自动机

看上去像是回文串自动机的模板题,就来了一发 The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1992    Accepted Submission(s): 694 Problem Description Now, you are given a string S. We want

hdu 3948 The Number of Palindromes

The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)http://acm.hdu.edu.cn/showproblem.php?pid=3948 Problem Description Now, you are given a string S. We want to know how many distinct substri