码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 570 E. Pig and Palindromes

时间:2015-08-17 23:50:18      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出n*m的字母表,求从左上角走到右下角能形成多少个回文串,只能往下或往右走。


做法:dp[r1][c1][r2][c2],从左上角走到(r1,c1),从右下角走到(r2,c2)时,能形成多少个回文串,因为爆内存,表示成dp[step][r1][r2],从左上角走到r1行,从右下角走到r2行,分别走了step步时,能形成多少个回文串,因为c1=step+2-r1,c2=n+m-step-r2,所以是一样的,这样差不多能过了,因为两边最多走250步,所以需要的空间是250*500*500,当然,由于step只跟step-1有关,所以也可以滚动数组。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
const int mod=1000000007;
char pic[501][501];
int dp[2][501][501];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%s",pic[i]);
	int len=(n+m)/2-1;
	for(int i=0;i<=len;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int c1=i+2-j;
			if(c1<=m)
			{
				for(int k=n;k>=j;k--)
				{
					int c2=n+m-i-k;
					dp[i&1][j][k]=0;
					if(c2>=c1&&c2<=m)
					{
						if(pic[j-1][c1-1]==pic[k-1][c2-1])
						{
							int id=(i&1),jd=(id^1);
							if(i==0)
								dp[id][j][k]=1;
							else
							{
								dp[id][j][k]=dp[jd][j][k];
								dp[id][j][k]=(dp[id][j][k]+dp[jd][j-1][k])%mod;
								dp[id][j][k]=(dp[id][j][k]+dp[jd][j][k+1])%mod;
								dp[id][j][k]=(dp[id][j][k]+dp[jd][j-1][k+1])%mod;
							}
						}
					}
				}
			}
		}
	}
	int id=(len&1),ans=0;
	for(int i=1;i<=n;i++)
		ans=(ans+dp[id][i][i])%mod;
	if((n+m-1)%2==0)
		for(int i=1;i<=n;i++)
			ans=(ans+dp[id][i][i+1])%mod;
	printf("%d",ans);
}



time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting ofn rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to n, and the columns — from left to right with numbers from 1 to m. Let‘s denote the cell at the intersection of the r-th row and the c-th column as (r,?c).

Initially the pig stands in cell (1,?1), and in the end she wants to be in cell (n,?m). Since the pig is in a hurry to get home, she can go from cell (r,?c), only to either cell (r?+?1,?c) or (r,?c?+?1). She cannot leave the forest.

The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to bebeautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem).

Count the number of beautiful paths from cell (1,?1) to cell (n,?m). Since this number can be very large, determine the remainder after dividing it by 109?+?7.

Input

The first line contains two integers n,?m (1?≤?n,?m?≤?500) — the height and width of the field.

Each of the following n lines contains m lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.

Output

Print a single integer — the number of beautiful paths modulo 109?+?7.

Sample test(s)
input
3 4
aaab
baaa
abba
output
3
Note

Picture illustrating possibilities for the sample test.

技术分享

技术分享

技术分享



版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 570 E. Pig and Palindromes

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/47731867

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!