uva10617 2010.8.1
来源:互联网 发布:知行小学在大连排名 编辑:程序博客网 时间:2024/05/18 15:54
10617 - Again Palindrome
Again Palindromes
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
A palindorme is a sequence of one or morecharacters that reads the same from the left as it does from the right. Forexample, Z, TOT and MADAM are palindromes, but ADAM is not.
Given a sequence S of N capital latinletters. How many ways can one score out a few symbols (maybe 0) that the restof sequence become a palidrome. Varints that are only different by an order of scoring out shouldbe considered the same.
Input
The input file contains several test cases(less than 15). The first line contains an integer T that indicates how manytest 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 linean integer – the number of ways.
Sample Input Output for Sample Input
3
BAOBAB
AAAA
ABA
22
15
5
Russian Olympic Camp
#include <cstdio>#include <cstring>#define MAXN 70long long f[MAXN][MAXN];int n;char s[MAXN];void dpit(){ memset(f,0,sizeof(f));for(int i=1;i<=n;i++)f[i][i]=1;for(int p=1;p<=n;p++)for (int i=1;i<=n;i++){int j=i+p;if (j>n) break; f[i][j]=f[i+1][j-1]+2;for(int k=i+1;k<=j;k++)if (s[i]==s[k])f[i][j]+=f[i+1][k-1]+1;for(int k=i;k<=j-1;k++)if (s[j]==s[k])f[i][j]+=f[k+1][j-1]+1;if (s[i]==s[j])f[i][j]=f[i][j]-1-f[i+1][j-1];//printf("%d %d %d\n",i,j,f[i][j]);}}int main(){int t,ans;scanf("%d",&t);getchar();while (t--){gets(s+1);n=strlen(s+1);dpit();printf("%lld\n",f[1][n]);}return 0;}
- uva10617 2010.8.1
- uva10617
- uva10617(DP)
- uva10617 - Again Palindrome(dp)
- UVA10617- Again Palindrome
- uva10617 - Again Palindrome(动归)
- uva10617 - Again Palindrome(记忆化搜索)
- 网络日志(2010.8.1)
- poj1390 2010.8.1
- poj2231 2010.8.1
- 2010.8.7
- 2010.8.18
- 2010.8.23
- 2010.8.23
- 2010.8 DeCoder
- 2010.8.26
- 网络日志(2010.8.14)
- 网络日志(2010.8.20)
- 不容易系列之(3)—— LELE的RPG难题
- poj2231 2010.8.1
- POJ 2771 Guardian of Decency(二分图最大独立集)
- 1026. 程序运行时间
- NYOJ-71-独木舟上的旅行
- uva10617 2010.8.1
- 145. Binary Tree Postorder Traversal LeetCode
- poj1390 2010.8.2
- 字符串-《数据结构1000》-练习题
- 打印函数调用栈
- Bravo!Java学习笔记(1)---接口、抽象类、对象克隆、内部类
- java&javaweb学习笔记(汇总)
- 回文字符
- 栈-《数据结构1000》-练习题