Again Palindromes - UVa 10617 dp
来源:互联网 发布:安畅网络财报 编辑:程序博客网 时间:2024/05/19 13:14
Problem I
Again Palindromes
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
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, TOTand 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 Output for Sample Input
3
BAOBAB
AAAA
ABA
22
15
5
题意:删除字符串中任意数量的字符,使得其成为一个回文串,问这种删法一共有多少种,不删也算一种。
思路:还是从边上往中间递推if(s[l]==s[r]) dp[l][r]=dp[l+1][r]+dp[l][r-1]+1; else dp[l][r]=dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1];
AC代码如下:
#include<cstdio>#include<cstring>using namespace std;long long dp[100][100],;char s[110];void dfs(int l,int r){ if(dp[l][r]>=0) return; if(l>r) { dp[l][r]=0; return; } dfs(l+1,r); dfs(l,r-1); dfs(l+1,r-1); if(s[l]==s[r]) dp[l][r]=dp[l+1][r]+dp[l][r-1]+1; else dp[l][r]=dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1];}int main(){ int t,n,m,i,j,k,len; scanf("%d",&t); while(t--) { scanf("%s",s+1); len=strlen(s+1); memset(dp,-1,sizeof(dp)); for(i=1;i<=len;i++) dp[i][i]=1; dfs(1,len); printf("%lld\n",dp[1][len]); }}
- uva 10617 Again Palindromes(DP)
- Again Palindromes - UVa 10617 dp
- UVA 10617 Again Palindromes (区间dp)
- Uva 10617 Again Palindromes
- Again Palindromes+uva+回文串dp
- UVa 10617 Again Palindromes / 记忆化搜索
- uva 10617 - Again Palindrome(dp)
- UVA 10617 Again Palindrome --dp
- UVa 10617 - Again Palindrome 字符串dp
- Uva 10617 Again Palindrome(区间dp)
- uva 10617 Again Palindrome (DP)
- UVa 10617 Again Palindrome(回文 区间dp)
- Again Palindromes
- Uva 10617 Again Palindrome (区间DP+回文串)
- UVa 10617 Again Palindrome(回文串区间DP)
- UVA 题目10617 - Again Palindrome(区间DP)
- uva 11584 - Partitioning by Palindromes (dp)
- uva 11584 - Partitioning by Palindromes(简单dp)
- 一个数据挖掘的开源工具——Weka
- groovy入门 第08章 流程控制
- html 汉字空格占位符
- 使用cocos2d-x3.0开发一个极其简易的求签测运小软件——萌签[更新中]
- mmc子系统学习笔记五 mmc子系统的代码组织
- Again Palindromes - UVa 10617 dp
- cocos2d-x 3.x : 使用VS2012/2013调试 Cocos2d-x + Lua (赞哦, 很不错)
- JQuery Easy-UI 实现动态分页
- 热门免费Java游戏
- 测试你的手机的Java ENVIROMENT
- centos添加用户
- SDIO相关学习
- 1407311504-hd-不容易系列之二.cpp
- JPasswordField的乱码问题