题目描述
Snuke has two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).
There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is Si,j. On the second board, nothing is written yet.
Snuke will write letters on the second board, as follows:
First, choose two integers A and B ( 0≤A,B<N ).
Write one letter in each square on the second board. Specifically, write the letter written in Square (i+A,j+B) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.
After this operation, the second board is called a good board when, for every i and j ( 1≤i,j≤N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.
Find the number of the ways to choose integers A and B ( 0≤A,B<N ) such that the second board is a good board.
Constraints
1≤N≤300
Si,j ( 1≤i,j≤N ) is a lowercase English letter.
There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is Si,j. On the second board, nothing is written yet.
Snuke will write letters on the second board, as follows:
First, choose two integers A and B ( 0≤A,B<N ).
Write one letter in each square on the second board. Specifically, write the letter written in Square (i+A,j+B) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.
After this operation, the second board is called a good board when, for every i and j ( 1≤i,j≤N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.
Find the number of the ways to choose integers A and B ( 0≤A,B<N ) such that the second board is a good board.
Constraints
1≤N≤300
Si,j ( 1≤i,j≤N ) is a lowercase English letter.
输入
Input is given from Standard Input in the following format:
N
S1,1S1,2..S1,N
S2,1S2,2..S2,N
:
SN,1SN,2..SN,N
N
S1,1S1,2..S1,N
S2,1S2,2..S2,N
:
SN,1SN,2..SN,N
输出
Print the number of the ways to choose integers A and B ( 0≤A,B<N ) such that the second board is a good board.
样例输入
复制样例数据
2
ab
ca
样例输出
2
提示
For each pair of A and B, the second board will look as shown below:
***
The second board is a good board when (A,B)=(0,1) or (A,B)=(1,0), thus the answer is 2.
扩展成(2n-1)*(2n-1),计算扩展后的正方形内有多少长度为n的子方形,dp[i][j]表示使以i,j为右下脚坐标的子方形呈对称的最大边长。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char Map[605][605]; int dp[605][605]; int n,A,B; int main() { scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf(" %c",&Map[i][j]); Map[i+n][j]=Map[i][j+n]=Map[i+n][j+n]=Map[i][j]; } } int ans=0; for(int i=1;i<=2*n-1;i++){ for(int j=1;j<=2*n-1;j++){ int cnt=0; for(int k=1;k<=min(i-1,j-1);k++){ if(Map[i][j-k]==Map[i-k][j]) cnt++; else break; } dp[i][j]=1+min(dp[i-1][j-1],cnt); if(dp[i][j]>=n) ans++; } } printf("%d\n",ans); return 0; }