UVA10617 Again PalindromeDP
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10617 Again PalindromeDP相关的知识,希望对你有一定的参考价值。
A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, Z, TOT and MADAM are palindromes, but ADAM is not.
Given a sequence S of N capital latin letters. How many ways can one score out a few symbols (maybe 0) that the rest of sequence become a palidrome. Varints that are only different by an order of scoring out should be considered the same.
Input
The input file contains several test cases (less than 15). The first line contains an integer T that indicates how many test cases are to follow.
Each of the T lines contains a sequence S (1 ≤ N ≤ 60). So actually each of these lines is a test case.
Output
For each test case output in a single line an integer — the number of ways.
Sample Input
3
BAOBAB
AAAA
ABA
Sample Output
22
15
5
问题链接:UVA10617 Again Palindrome
问题简述:给定若干字符串,可以删除字符串中的字符,计算每个字符串中包含的回文数量。
问题分析:用DP来解决,给出2个题解程序,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10617 Again Palindrome */
#include <bits/stdc++.h>
using namespace std;
const int N = 60 + 1;
long long dp[N][N];
char s[N + 1];
int main()
{
int t;
scanf("%d", &t);
while (t--) {
scanf("%s", s + 1);
int len = strlen(s + 1);
memset(dp, 0, sizeof dp);
for (int i = 1; i <= len; i++) dp[i][i] = 1;
for (int i = len - 1; i >= 1; i--)
for (int j = i + 1; j <= len; j++) {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
if(s[i] == s[j])
dp[i][j] += dp[i + 1][j - 1] + 1;
}
printf("%lld\\n", dp[1][len]);
}
return 0;
}
AC的C++语言程序如下:
/* UVA10617 Again Palindrome */
#include <bits/stdc++.h>
using namespace std;
const int N = 60 + 1;
long long dp[N][N];
char s[N];
int main()
{
int t;
scanf("%d", &t);
while (t--) {
scanf("%s", s);
int len = strlen(s);
memset(dp, 0, sizeof dp);
for (int i = 0; i < len; i++)
for (int j = 0; j + i < len; j++)
if (i == 0)
dp[j][j] = 1;
else if (s[j] == s[j + i])
dp[j][j + i] = dp[j][j + i - 1] + dp[j + 1][j + i] + 1;
else
dp[j][j + i] = dp[j][j + i - 1] + dp[j + 1][j + i] - dp[j + 1][j + i - 1];
printf("%lld\\n", dp[0][len - 1]);
}
return 0;
}
以上是关于UVA10617 Again PalindromeDP的主要内容,如果未能解决你的问题,请参考以下文章
UVa 10780 - Again Prime? No Time