CF 316 E. Pig and Palindromes 求左上角走到右下角是回文的方法数 DP
来源:互联网 发布:大数据培训好就业吗 编辑:程序博客网 时间:2024/06/02 01:17
题意:给一个n*m的格子,每个格子有一个字母,只有向下和向左两种走法,现在要求,从左上角走到右下角,走过的格子的字母是回文的有多少种走法?
DP,枚举步数,因为是回文串,所以应该是对称的,步数应该为
代码:
//author: CHC//First Edit Time: 2015-08-29 15:06#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <vector>#include <map>#include <queue>#include <set>#include <algorithm>#include <limits>using namespace std;typedef long long LL;const int MAXN=510;const int INF = numeric_limits<int>::max();const LL LL_INF= numeric_limits<LL>::max();const int mod=1e+9 + 7;LL dp[2][MAXN][MAXN];int n,m;char mapp[MAXN][MAXN];bool check(int x){ return x>=1&&x<=n;}int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++) scanf(" %s",mapp[i]+1); int k=0; for(int step=(n+m)/2;step>0;step--){ k^=1; for(int c1=1;c1<=m;c1++){ for(int c2=c1;c2<=m;c2++){ int r1=step-c1+1; int r2=n+m-step-c2+1; if(!(check(r1)&&check(r2)&&r1<=r2))continue; //printf("step:%d %d %d %d %d\n",step,r1,c1,r2,c2); if(mapp[r1][c1]==mapp[r2][c2]){ if(r1==r2&&c1==c2)dp[k][c1][c2]=1; else if(r1==r2&&c1+1==c2)dp[k][c1][c2]=1; else if(r1+1==r2&&c1==c2)dp[k][c1][c2]=1; else dp[k][c1][c2]=(dp[k^1][c1+1][c2]+dp[k^1][c1+1][c2-1]+dp[k^1][c1][c2-1]+dp[k^1][c1][c2])%mod; } else dp[k][c1][c2]=0; } } } printf("%I64d\n",dp[k][1][m]%mod); } return 0;}
0 0
- CF 316 E. Pig and Palindromes 求左上角走到右下角是回文的方法数 DP
- 【cf 570e】Pig and Palindromes dp
- CF 316div2 E.Pig and Palindromes
- Codeforces #316 E Pig and Palindromes DP
- codeforces #316 E. Pig and Palindromes (dp~)
- (hdu step 3.2.8)命运(简单DP:求从左上角走到右下角的最大值)
- 【CF 570E】Pig and Palindromes
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes DP
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes (DP)
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes dp
- Codeforces 570E Pig and Palindromes dp
- E. Pig and Palindromes
- Codeforces 570E - Pig and Palindromes (DP)
- Codeforces Round #316 (Div. 2)E. Pig and Palindromes(dp好题)
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes、HDU 5617 Jam's maze(dp)
- CodeForces570E - Pig and Palindromes 【DP】
- 求矩阵左上角到右下角路径的最大价值
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes
- ios中创建可以拖动的view原理和实现详解(含代码)
- 关于spring依赖注入的一个知识点
- Qt 学习之路 2(1):序 笔记
- UI 18 单例
- github提交可能遇到的问题
- CF 316 E. Pig and Palindromes 求左上角走到右下角是回文的方法数 DP
- android 两次点击back键退出程序
- Block方法与Block属性结合使用的小实现
- 移植lame-3.99.5库
- uva 11401 Triangle Counting 数三角形 (计数问题)
- LeetCode(257)Binary Tree Paths
- 字符设备控制
- win7自带IE浏览器图标如何删除
- 利用jquery完成表单域的收集,利用ajax实现信息传递给服务器存储