原题下载
答案
(Analysis by Nick Wu)
This is a DP problem where we iteratively count the number of palindromes that we can build from the middle.
Let f(a,r1,r2) be the number of palindromic strings that we can build of length 2a+1, where the start of the string is on row r1, the end of the string is on row r2, and the middle of the string is on the diagonal of the grid that goes from the top-right to the bottom-left of the grid. We initialize f(0,i,i)=1 for all possible rows. Because of the constraints of the DP state, the beginning and ending squares are uniquely determined by their row. Therefore, f(a,r1,r2) affects at most four other quantities:f(a+1,r1,r2), f(a+1,r1−1,r2),f(a+1,r1,r2+1), and f(a+1,r1−1,r2+1).
This gives an O(N^3) algorithm which can be implemented in O(N^2) memory because you only need to keep track of f(a,r1,r2) and f(a+1,r1,r2) concurrently over all possible pairs (r1,r2).
Here is my code.
import java.io.*;
import java.util.*;
public class palpathG {
static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("palpath.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("palpath.out")));
n = Integer.parseInt(br.readLine());
char[][] grid = new char[n][n];
for(int i = 0; i < n; i++) {
String s = br.readLine();
for(int j = 0; j < n; j++) {
grid[i][j] = s.charAt(j);
}
}
long[][] dp = new long[n][n];
for(int i = 0; i < n; i++) {
dp[i][i] = 1;
}
final long MOD = 1000000007;
for(int num = n-1; num >= 1; num--) {
long[][] next = new long[n][n];
for(int a = 0; a < n; a++) {
int rowA = a;
int colA = (num-1-a);
if(colA < 0) continue;
for(int b = 0; b < n; b++) {
int rowB = b;
int colB = 2*n-num-rowB-1;
if(colB >= n) continue;
if(grid[rowA][colA] != grid[rowB][colB]) continue;
next[rowA][rowB] += dp[rowA][rowB];
if(rowA+1 < n) next[rowA][rowB] += dp[rowA+1][rowB];
if(rowB-1 >= 0) next[rowA][rowB] += dp[rowA][rowB-1];
if(rowA+1 < n && rowB-1 >= 0) next[rowA][rowB] += dp[rowA+1][rowB-1];
next[rowA][rowB] %= MOD;
}
}
dp = next;
}
pw.println(dp[0][n-1]);
pw.close();
}
}
以上就是关于【USACO 2015 US Open, Gold Problem 2. Palindromic Paths】的解答,如需了解学校/赛事/课程动态,可至翰林教育官网获取更多信息。
往期文章阅读推荐:
AMC/BPhO/UKChO…下半年20+赛事集中开考!抓住竞赛"黄金备赛期"!
2027 USAAIO人工智能奥赛报名进行中!Round 1新规出炉,2月开赛如何备考?

© 2026. All Rights Reserved. 沪ICP备2023009024号-1