hdu 4632 Palindrome subsequence (区间DP+容斥)
来源:互联网 发布:淘宝开店需要交保证金 编辑:程序博客网 时间:2024/05/16 03:29
Problem Description
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence < A, B, D> is a subsequence of < A, B, C, D, E, F>.
(http://en.wikipedia.org/wiki/Subsequence)
Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = < Sx1, Sx2, …, Sxk> and Y = < Sy1, Sy2, …, Syk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also two subsequences with different length should be considered different.
Input
The first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.
Output
For each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.
Sample Input
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960
题意:找到一个字符串里又多少个回文串子序列;
思路:区间DP,dp[i][j]表示i到j的回文串数,依次从小到大的区间开始,我们需要判断每个区间两个边缘字符是否相等+容斥原理便可以推出结果;
转移状态
**if(c[i]==c[j])
dp[i][j]=(dp[i+1][j]+dp[i][j-1]+1)%10007;
else
dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1])%10007;**
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[1005][1005];int main(){ int T; scanf("%d",&T); char c[1005]; for(int t=1;t<=T;t++) { scanf("%s",c); memset(dp,0,sizeof(dp)); int n=strlen(c); for(int i=0;i<n;i++) dp[i][i]=1; for(int k=1;k<n;k++) for(int i=0,j=i+k;j<n;i++,j++) { if(c[i]==c[j]) dp[i][j]=(dp[i+1][j]+dp[i][j-1]+1+10007)%10007; else dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007; } printf("Case %d: %d\n",t,dp[0][n-1]); }}
- hdu 4632 Palindrome subsequence (区间DP+容斥)
- HDU 4632 Palindrome subsequence (区间dp 容斥定理)
- HDU 4632 Palindrome subsequence(区间dp)
- HDU 4632 Palindrome subsequence (区间DP)
- HDU 4632 Palindrome subsequence(区间DP)
- HDU 4632 Palindrome subsequence(区间dp)
- HDU 4632 - Palindrome subsequence(区间DP)
- HDU 4632 Palindrome subsequence (区间DP)
- HDU 4632 Palindrome subsequence(区间DP)
- HDU 4632 Palindrome subsequence (区间DP)
- HDU 4632 Palindrome subsequence(区间dp)
- HDU Palindrome subsequence(区间DP)
- Hdu oj 4632 Palindrome subsequence(区间dp)
- hdu 4632 Palindrome subsequence(DP)
- hdu - 4632 - Palindrome subsequence(dp)
- 【HDU】4632 Palindrome subsequence (DP)
- hdu 4632 Palindrome subsequence(dp)
- Palindrome subsequence(区间dp)
- 禁止浏览器主页自动跳转hao123等流氓网站
- POJ 2431 贪心
- 198彩娱乐平台主管收有量总代Q1440580
- [Servlet]会话追踪技术
- [leetcode] 35. Search Insert Position
- hdu 4632 Palindrome subsequence (区间DP+容斥)
- Codeforces Round #367 (Div. 2) Vasiliy's Multiset xor trie
- Codeforces 706C Hard problem(dp+字符串)
- 制作mesos的docker镜像(centos7.2)
- [leetcode] 89. Gray Code
- 基于NX的研发产品设计管理平台实现(七)--BOM的设计与管理2
- 零散记8-10~8-11
- react native redux
- [leetcode] 62. Unique Paths