描述

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)的更多相关文章

  1. 5. Longest Palindromic Substring (DP)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. CF750G New Year and Binary Tree Paths(DP)

    神仙题.为啥我第一眼看上去以为是个普及题 路径有两种,第一种是从 LCA 一边下去的,第二种是从 LCA 两边都下去了的. 先考虑第一种. 先枚举路径长度 \(h\). 当 LCA 编号是 \(x\) ...

  3. 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) ...

  4. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  5. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  6. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

  7. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  8. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  9. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

随机推荐

  1. eureka集群环境搭建

    一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eurek ...

  2. Vue.js - day6

    注意: 有时候使用npm i node-sass -D装不上,这时候,就必须使用 cnpm i node-sass -D 在普通页面中使用render函数渲染组件 在webpack中配置.vue组件页 ...

  3. pingall脚本

    p i n g a l l:一个按照/ e t c / h o s t s文件中的条目逐一p i n g所有主机的脚本 它能够按照/ e t c / h o s t s文件中的条目逐一p i n g所 ...

  4. Longest Increasing Subsequence的两种算法

    问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7-.an,求它的一个子序列(设为s1,s2,-sn),使得这个子序列满足这样的性质,s1<s2<s3<-<sn并且 ...

  5. Linux中grep、sed、awk使用介绍

    linux文件操作命令介绍1)grepgrep 用于在文件中查找符合条件的记录grep 参数 过滤条件 文件过滤的条件中可使用正则表达式-c 显示符合的行数-i 忽略大小写-n 显示符合要求的记录,包 ...

  6. Entity Framework插入数据报错:Validation failed for one or more entities

    www.111cn.net 编辑:lanve 来源:转载 今天在处理Entity Framework插入数据库时,报错: Validation failed for one or more entit ...

  7. Codeforces Round #318 (Div. 2) C Bear and Poker (数学)

    简单题,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. 求lcm,爆了long long.我得好好反省一下,对连乘不敏感 #include<bits/stdc++.h&g ...

  8. Google Colab的一些注意事项

    1.执行命令行前面加! 当我们使用python解释器时,我们需要不停地在命令行和IDE 之间切换,当我们需要使用命令行工具时.不过,Jupyter Notebook给了我们在notebook中运行sh ...

  9. ES6新增Map、Set和iterable

    Map需要一个二维数组 var test_map = new Map(["mians",99],["regink",88]) test_map.get(&quo ...

  10. Leetcode 71 简化路径simplify-path(栈)

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如,path = "/home/", => "/home"path = " ...