回文序列(P1221)
来源:互联网 发布:37周胎儿b超数据 编辑:程序博客网 时间:2024/05/20 00:11
题解只写给新手看,大牛略过。
最近一直在作动态规划的题,但这道还是想了好久,水啊。
先研究一下每一组数据时怎样由前面的推出的。
比如 6的 (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3), (1 2 2 1), ( 1 1 1 1 1 1) 一共7个。将6拆成两个相同的数和另一个大于这个数的数或者0(真拗口)。直接看下面就好:
1、6=1+1+4。那么他可以将4的所有情况两边都加1变成6的解 (1 4 1), (1 1 2 1 1), (1 2 2 1), ( 1 1 1 1 1 1)共4个。
2、6=2+2+2。那么他可以将2的所有最小值大于等于2的情况两边加2 即将(2) 变成 (2,2,2) 共1个
3、6=3+3+0。注意只有另一个数是0的时候才可以这么拆 如 5= 2+2+1 是不可以的。那么他可以将0 的所有情况两边加3,变成3,3 共1个。这里默认0的情况总数是1.
4、6本身自己 (6) 就是一组 共1个
所以 6的解共4+1+1+1=7个。
由以上分析得到状态转移方程 s[i][j] = s[i-2*j][j] + s[i][j+1];(1<=j<=i/2)
其中i表示数据的和,就是题中的n,j表示i的所有解中所有数大于等于j的情况总数(之所以这样定义可以参考上面分析中的第2步)
具体实现
1、将s[i][j] (i>=j>i/2) 内容预处理成1(因为对于j>i/2时 所有的s[i][j]都是1,如s[6][4]=s[6][5]=s[6][6]=1 只有(6)一个)。
2、将s[i][j] (i<j) 内容预处理成0 (因为根据定义j不可能比i大。)
3、将s[0][j] 内容预处理成1 (因为当需要调用s[0][j]时表示一个数拆成了完全相同的两个数,结果当然是一个了,)
4、在for (i=2;i<=nax;i++)的每次循环中加入for (j=i/2;j>=1;j--)循环,每次逆推结果
程序实现过程
比如i=6时
1、j=3 s[6][3] = s[6-3*2][3] + s[6][4] = 1 + 1 =2 ; 表示6中解大于3的有两种(3,3) (6);
2、j=2 s[6][2] = s[2][2] + s[6][3] = 1 + 2 =3;
3、j=1 s[6][1] = s[4][1] + s[6][2] = 4 + 3 =7; s[6][1]中储存n=6时的所有解
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<set>#include<cstdlib>#include<cstring>#include<stack>#include<string>using namespace std;#define N 300long long dp[N][N];int main(){int i,j,k;int n;for (i=0;i<N;i++){dp[0][i]=1;}dp[1][1]=1;for (i=2;i<N;i++){for (j=i/2+1;j<=i;j++)dp[i][j]=1;for (j=i/2;j;j--)dp[i][j]=dp[i][j+1]+dp[i-2*j][j];}while (cin>>n,n)cout<<n<<' '<<dp[n][1]<<endl;return 0;}
UNIMODAL PALINDROMIC DECOMPOSITIONS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4235 Accepted: 2079
Description
A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),
(1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2),
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)
Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),
(1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2),
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)
Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.
Input
Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.
Output
For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page.
Sample Input
2345678102324131213920
Sample Output
2 23 24 45 36 77 58 1110 1723 10424 199131 5010688213 105585259092 331143
Source
- 回文序列(P1221)
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 统计回文子序列(动态规划)
- 最长回文子序列(LPS)
- 最长回文子序列(LCS)
- 回文字符串小结(回文串判定+最长回文子序列)
- 最长回文子序列
- 最长回文子序列
- 【数据库】mysql使用分享(不断更新)
- MyEclipse连接MySQL的方法
- 挂载错误
- 一套的实用完整Makefile参考模板
- 设置快捷键 ctrl+N
- 回文序列(P1221)
- 常用软件备忘录
- linux profile
- 正则表达式中的贪婪与懒惰
- IDA 远程调试
- 使用批处理文件,自动设置计算机IP地址
- 颜色
- DX帧数的检测与设置
- WPF 像VS2010 启动时加载页面的进度条