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]

    用到了容斥原理。

    pal[x][y]表示子串s[x to y]是否是一个回文串(是为1,不是为0)。

    其中pal[x][y]又有递推式:

    pal[x][y] = ( s[x]==s[y] && (x+1==y || pal[x+1][y-1]) )

    记忆化搜索就好啦。

  边界条件:

    if(x>y) dp[x][y] = 0

    if(x==y) dp[x][y] = 1

AC Code:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define MAX_N 5005
 5
 6 using namespace std;
 7
 8 int t;
 9 int dp[MAX_N][MAX_N];
10 int pal[MAX_N][MAX_N];
11 char s[MAX_N];
12
13 int is_pal(int x,int y)
14 {
15     if(s[x]!=s[y]) return 0;
16     if(x+1==y) return 1;
17     if(pal[x+1][y-1]!=-1) return pal[x+1][y-1];
18     return pal[x+1][y-1]=is_pal(x+1,y-1);
19 }
20
21 int dfs(int x,int y)
22 {
23     if(dp[x][y]!=-1) return dp[x][y];
24     if(x>y) return 0;
25     if(x==y) return dp[x][y]=pal[x][y]=1;
26     dp[x][y]=dfs(x,y-1)+dfs(x+1,y)-dfs(x+1,y-1);
27     pal[x][y]=(s[x]==s[y] && (x+1==y || pal[x+1][y-1]));
28     return dp[x][y]=dp[x][y]+pal[x][y];
29 }
30
31 int main()
32 {
33     scanf("%s%d",s+1,&t);
34     memset(dp,-1,sizeof(dp));
35     int x,y;
36     while(t--)
37     {
38         scanf("%d%d",&x,&y);
39         printf("%d\n",dfs(x,y));
40     }
41 }

原文地址:https://www.cnblogs.com/Leohh/p/8203478.html

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

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

「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

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

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

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

[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

Light OJ 1033 - Generating Palindromes(区间DP)

题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ======================================================================================= #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include&l

Codeforces 509F Progress Monitoring (区间dp 或 记忆化搜索)

F. Progress Monitoring time limit per test 1 second memory limit per test 256 megabytes Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students: You are given a tree T with n vertices, specified b

Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f