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 ...
随机推荐
- gulp的入门
http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/ http://www.ydcss.com/a ...
- 兼容IE的滚动条自定义样式
废话不多说,直接上: IE专属的滚动条样式定义,只能设置各种原始结构的颜色,宽高结构等其他样式无法修改: div{ scrollbar-arrow-color: red; /*三角箭头的颜色*/ sc ...
- 如何使用KeyChain保存和获取UDID
本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不变UDID的问题.并给出一个获取UDID的工具类,使用方便,只需要替换两个地方即可. 一.iOS ...
- Maven常见知识介绍
1)pom详解 2)pom详解 3)测试 4)插件与生命周期 5)maven生命周期 6)范围依赖
- MFC技术积累——基于MFC对话框类的那些事儿3
3.3.2 创建图形画刷来实现位图加载 1.首先在Resource View中导入一幅位图,位图大小96×96像素: 2.其次在主对话框中添加一个静态文本资源,ID号是IDC_BITMAPAREA,添 ...
- fun下载内容批量收集
1.download title and url #!/usr/bin/env python #-*- coding:utf-8 -*- import re, urllib2,threading de ...
- Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
今年中国掀起一股“苹果热”,智能手机iPhone.平板电脑iPad遭疯抢,一度卖断货.然而,令许多人意想不到的是,在“苹果”的老家——美国市场,智能手机中卖得最火的并不是iPhone,而是Androi ...
- TryEnterCriticalSection
if (TryEnterCriticalSection(&g_cs) == FALSE) { 返回 } else { 进入临界区 LeaveCriticalSection(&g_cs) ...
- UNIX 进程间通讯(IPC)概念(Posix,System V IPC)
IPC(Inter-Process Communication,进程间通讯)可以有三种信息共享方式(随文件系统,随内核,随共享内存).(当然这里虽然说是进程间通讯,其实也是可以和线程相通的). 相对 ...
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...