UVA 题目10617 - Again Palindrome(区间DP)

原创 2015年11月17日 17:33:53
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

题目大意:给一个字符串,问去掉其中几个字符变成回文串,问有多少种去法

思路:多少种去法,实际上就是问这个串里边有多少个回文串,编程之美资格赛的时候做个一个问一个串里边有多少个回文串的,感觉应该问的就是这个,提交,,就过了

ac代码

16449058 825 Walking on the Safe Side Accepted C++ 0.006 2015-11-17 08:21:59

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
long long dp[1010][1010];
char str[1005];
int main()
{
	int t,c=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",str);
		int i,len=strlen(str),j;
		for(i=0;i<len;i++)
		{
			dp[i][i]=1;
		}
		for(i=1;i<len;i++)
		{
			for(j=i-1;j>=0;j--)
			{
				dp[j][i]=(dp[j+1][i]+dp[j][i-1]-dp[j+1][i-1]);
				if(str[i]==str[j])
				{
					dp[j][i]+=dp[j+1][i-1]+1;
				}
			}
		}
		printf("%lld\n",dp[0][len-1]);
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

UVa 10617 Again Palindrome(回文 区间dp)

A palindorme is a sequence of one or more characters that reads the same from the left as it does fr...

Uva 10617 Again Palindrome(区间dp)

Again Palindromes Input: Standard Input Output: Standard Output Time Limit: 2 Seconds   A pal...

uva 10617 Again Palindrome(dp,容斥原理,求字符串内回文子序列的个数)

dp[i][j]表示s[i]到s[j]这一字串内回文串的个数

UVa 10617 - Again Palindrome 字符串dp

Problem I Again Palindromes Input: Standard Input Output: Standard Output Time Limit: 2 Second...
  • cyendra
  • cyendra
  • 2013年05月02日 20:19
  • 611

UVA 10617 Again Palindrome

uva 10617 Again Palindrome

UVA10739 String to Palindrome(区间DP + 递推)

/* Sample Input 6 tanbirahmed shahriarmanzoor monirulhasan syedmonowarhossain sadrulhabibchowdhury m...

uva 10739 - String to Palindrome(区间dp)

String to Palindrome Input: Standard Input Output: Standard Output Time Limit: 1 Second   In...

UVA 10453 Make Palindrome(区间dp-补全回文串+打印结果)

题目大意:给一个字符串,要求添加最少个字符,把它变成回文串,并输出。 思路: 区间dp,dp[i][j] 表示区间(i, j) 内的字符串添加的最少个数, 变成回文串那么, 如果str[...

uva10617(DP)

题目的意思就是给你一个字符串,你可以删除其中的一些字符,问有几种方法使他变成回文(什么都步删也算一种) 首先d[ i ][ j ]  代表从第i个 到第j 个变成回文,有几种办法. 我们可以知道 ...

uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=162...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 题目10617 - Again Palindrome(区间DP)
举报原因:
原因补充:

(最多只允许输入30个字)