Palindromic Paths(DP)
描述
Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example:
ABCD
BXZX
CDXB
WCBA
Each day, Susa walks from the upper-left field to the lower-right field, each step taking her either one field to the right or one field downward. Susa keeps track of the string that she generates during this process, built from the letters she walks across. She gets very disoriented, however, if this string is a palindrome (reading the same forward as backward), since she gets confused about which direction she had walked.
Please help Susa determine the number of distinct routes she can take that correspond to palindromes. Different ways of obtaining the same palindrome count multiple times. Please print your answer modulo 1,000,000,007.
输入
The first line of input contains N, and the remaining N lines contain the N rows of the grid of fields. Each row contains N characters that are in the range A...Z.
输出
Please output the number of distinct palindromic routes Susa can take, modulo 1,000,000,007.
样例输入
4
ABCD
BXZX
CDXB
WCBA
样例输出
12
提示
Susa can make the following palindromes
1 x "ABCDCBA"
1 x "ABCWCBA"
6 x "ABXZXBA"
4 x "ABXDXBA"
题目大意:
从左上角走到右下角(每次只能往右或往下)路径组成的串是回文串的路径数。
分析:可以让左下角和右上角同时开始走,dp[i][j][k]代表走i步左上角走到第j行,右上角走到第k行的回文数。dp[i][j][k]=dp[i-1][j-1][k]+dp[i-1][j-1][k+1]+dp[i-1][j][k]+dp[i-1][j][k+1],由于n最大为500,三维开不下,观察状态转移方程第i步只于第i-1步有关系,所以可以用滚动数组来优化。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MD=1e9+;
char s[][];
ll dp[][][];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",s[i]+);
int cnt=;
dp[cnt][][n]=;
for(int i=;i<=n;i++)
{
for(int x1=;x1<=n;x1++)
for(int x2=;x2<=n;x2++)
{
int y1=i-x1+,y2=*n-x2-i+;
if(y1<=||y1>n||y2<=||y2>n) continue;
if(s[x1][y1]==s[x2][y2])
dp[cnt^][x1][x2]=(dp[cnt][x1-][x2]+dp[cnt][x1-][x2+]+dp[cnt][x1][x2]+dp[cnt][x1][x2+])%MD;
}
for(int i=;i<=n;i++)///很重要
for(int j=;j<=n;j++)
dp[cnt][i][j]=;
cnt^=;
}
ll ans=;
for(int i=;i<=n;i++)
ans+=dp[cnt][i][i];
printf("%I64d\n",ans%MD);
return ;
}
Palindromic Paths(DP)的更多相关文章
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- CF750G New Year and Binary Tree Paths(DP)
神仙题.为啥我第一眼看上去以为是个普及题 路径有两种,第一种是从 LCA 一边下去的,第二种是从 LCA 两边都下去了的. 先考虑第一种. 先枚举路径长度 \(h\). 当 LCA 编号是 \(x\) ...
- Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)
题目链接:https://codeforces.com/contest/1366/problem/C 题意 有一个 $n \times m$ 的 $01$迷宫,要使从 $(1,1)$ 到 $(n,m) ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
随机推荐
- JVM垃圾回收机制三
垃圾回收器 分代垃圾回收常见的垃圾回收器 判断一个垃圾回收器好坏的标准 1.吞吐量越高越好 2.工作线程暂停时间越短越好. Serial垃圾回收器 串行回收器时最古老的最基本的垃圾回收器,工作线程会产 ...
- WebService学习之旅(七)Axis2发布WebService的几种方式
前面几篇文章中简单的介绍了如何使用Axis2发布WebService及如何使用Axis2实现Web服务的客户端调用,本节將详细介绍Axis2发布WebService的几种方式. 一.使用aar包方式发 ...
- 解决更新到os x10.11后openssl头文件无法找到的问题
os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...
- js数组去重方法包括Es6(方法有很多,但是需要考虑兼容性和数据类型场景)
1.Es6提供的方法 <script type="text/javascript"> //ES6里新添加了两个方法,set(set是一种新的数据结构,它可以接收一个数组 ...
- spring 配置多个properties
复制多份,保证有效的配置文件,属性时true就行 <bean class="org.springframework.beans.factory.config.PropertyPlace ...
- 洛谷 P1340 兽径管理
题目描述 约翰农场的牛群希望能够在 N 个(1<=N<=200) 草地之间任意移动.草地的编号由 1到 N.草地之间有树林隔开.牛群希望能够选择草地间的路径,使牛群能够从任一 片草地移动到 ...
- android 焦点 ListView 点击事件获取失败
1. 在ListView 中, 创建一个app_item.xml 布局文件 在布局文件中有如下的代码: <CheckBox android:id="@+id/cb_t ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...
- java导入Excel表格数据
首先导入Excel数据需要几样东西 第一需要两个依赖包,这里直接是在pom注入依赖 <!--excel--> <dependency> <groupId>org.a ...
- No package python-pip available
因为没有此rpm包,此包包含在epel源里面 输入rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarc ...