TOJ 5020: Palindromic Paths

5020: Palindromic Paths 分享至QQ空间

Time Limit(Common/Java):10000MS/30000MS     Memory Limit:65536KByte
Total Submit: 8            Accepted:4

Description

 

 

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.

 

 

 

Input

 

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.

 

 

Output

 

 

Please output the number of distinct palindromic routes Susa can take, modulo 1,000,000,007.

 

 

Sample Input

4
ABCD
BXZX
CDXB
WCBA

 

Sample Output

 12

Hint

 

Susa can make the following palindromes

1 x "ABCDCBA"

1 x "ABCWCBA"

6 x "ABXZXBA"

4 x "ABXDXBA"

 

Source

USACO 2015 US Open

一道不错的枚举+滚动数组,美滋滋,f[i][j][k]表示第一个点在第i行,第2个点在第j行都走了k步的方案数

 

#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const int mod=1e9+7;
char s[502][502];
ll dp[502][502][2];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%s",s[i]+1);
    int now=1,pre=0;
    if(s[1][1]!=s[n][n]) {
        return 0,printf("0\n");
    }
    dp[1][n][pre]=1;
    for(int k=2; k<=n; k++) {
        for(int i=1; i<=k; i++)
            for(int j=n; j>=i&&j>=n-k+1; j--) {
                if(s[i][k-i+1]==s[j][n-k+n-j+1])
                    dp[i][j][now]=(dp[i-1][j][pre]+dp[i][j][pre]+dp[i][j+1][pre]+dp[i-1][j+1][pre])%mod;
                else dp[i][j][now]=0;
            }
        swap(now,pre);
    }
    ll ans=0;
    for(int i=1; i<=n; i++) {
        ans=(ans+dp[i][i][pre])%mod;
    }
    printf("%lld",ans);
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/BobHuang/p/7277296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值