Uva 10617 Again Palindrome (区间DP+回文串)
来源:互联网 发布:淘宝otc药货到付款 编辑:程序博客网 时间:2024/05/17 01:28
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,TOT and MADAM are palindromes, butADAM 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 integerT 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
Output for Sample Input
22
15
5
算i+1,j-1的回文串数,所以要减去,另外,因为str[ i ]==str[ j ],所以i+1,j-1之间的回文串也能
和str[ i ],str[ j ]组成回文串,所以还要加上i+1,j-1之间的回文串。另外,str[ i ]与str[ j ]自身也
组成了回文串。
#include <iostream>#include <string>#include <cstdio>#include <cstring>using namespace std;const int maxn=65;string str;long long dp[maxn][maxn];int len;void initial(){ for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) { if(i==j) dp[i][j]=1; else dp[i][j]=0; }}void input(){ cin>>str; len=str.length();}void solve(){ for(int i=2; i<=len; i++) for(int j=0,k=i-1; k<len; j++,k++) { dp[j][k]=dp[j+1][k]+dp[j][k-1]; if(str[j]==str[k]) dp[j][k]+=1; else dp[j][k]-=dp[j+1][k-1]; } printf("%lld\n",dp[0][len-1]);}int main(){ int T; scanf("%d",&T); while(T--) { initial(); input(); solve(); } return 0;}
- 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)
- UVA 10617 Again Palindrome --dp
- UVA - 10617 Again Palindrome 字符串的回文子串
- UVA 10617 Again Palindrome 又是回文 dp,记忆化搜索
- uva 10617 Again Palindrome(dp,容斥原理,求字符串内回文子序列的个数)
- UVa 10617 - Again Palindrome 字符串dp
- uva 10617 Again Palindrome (DP)
- Again Palindromes+uva+回文串dp
- UVA 10617 Again Palindromes (区间dp)
- UVA 10453 Make Palindrome(区间dp-补全回文串+打印结果)
- String to Palindrome+uva+回文串dp
- Make Palindrome+uva+回文串dp
- uva 10617 Again Palindrome
- 在现有SSH项目中提供REST方式的WebService
- PHP实现装饰模式
- 2013年12月 那年的数据导出
- sass揭秘之@mixin,%,@function
- 二叉排序树的基本操作测试
- Uva 10617 Again Palindrome (区间DP+回文串)
- 当Webservice已经绑定了一个IP后再次修改IP时怎么设置
- x264源码分析 -- x264_slicetype_path
- 第九周项目二 分数的累加 第二种做法
- ArcGIS教程:ArcGIS栅格图像矢量配准
- Content Provider的加载
- JFrame
- 阶乘
- iOS: ARC和非ARC下使用Block属性的问题