Palindrome subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Others)Total Submission(s): 1330 Accepted Submission(s): 547
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 = <S x1, S x2, ..., S xk> and Y = <S y1, S y2, ..., S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. Also two subsequences with different length should be considered different.
(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 = <S x1, S x2, ..., S xk> and Y = <S y1, S y2, ..., S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. 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[l][r]表示区间[l,r]的最多回文子串数。转移方程:
dp[l][r]=dp[l][r-1]+dp[l+1][r]+(s[l]==s[r]?1:-dp[l+1][r-1])
特别注意的是由于取模的原因,减法算出来可能会有负数。此时应该先加上mod再取模
AC代码(队友):
#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 1010
#define mod 10007
using namespace std;
char s[N];
int dp[N][N];
int main() {
int cas, cases = 1;
int i, j, k;
scanf("%d", &cas);
while (cas--) {
scanf("%s", s);
int len = strlen(s);
memset(dp, 0, sizeof(dp));
for (i = 0; i < len; i++)
dp[i][i] = 1;
for (i = 0; i < len; i++)
for (j = i - 1; j >= 0; j--) {
dp[j][i] = (dp[j][i - 1] + dp[j + 1][i] - dp[j + 1][i - 1] + mod) % mod;
if (s[i] == s[j]) dp[j][i] = (dp[j][i] + dp[j + 1][i - 1] + 1) % mod;
}
printf("Case %d: %d\n", cases, dp[0][len - 1]);
cases++;
}
return 0;
}
赛后自己写的记忆化搜索:
第一次知道记忆化搜索效率可以比非递归的dp慢这么多,本程序跑了921ms而dp只需320ms就能过了……
#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 1001
#define mod 10007
using namespace std;
char s[N];
int f[N][N];
int dfs(int l, int r) {
if (l > r) return 0;
if (l == r) return 1;
if (f[l][r] != -1) return f[l][r];
if (r - l == 1) return f[l][r] = (s[l] == s[r] ? 3 : 2);
if (r - l == 2) {
f[l][r] = (s[l] == s[l + 1] ? 3 : 2) + (s[r - 1] == s[r] ? 3 : 2);
if (s[l] == s[r]) f[l][r]++;
else f[l][r]--;
return f[l][r];
}
f[l][r] = (dfs(l + 1, r) + dfs(l, r - 1));
if (s[l] == s[r]) f[l][r]++;
else f[l][r] -= dfs(l + 1, r - 1);
if (f[l][r] >= mod) f[l][r] -= mod;
else if (f[l][r] < 0) f[l][r] += mod; //此处如果用%mod果断超时
return f[l][r];
}
int main() {
int T;
int id = 1;
scanf("%d", &T);
while (T--) {
memset(f, -1, sizeof(f));
scanf("%s", s);
printf("Case %d: %d\n", id++, dfs(0, strlen(s) - 1));
}
return 0;
}