@Sonya and Matrix Beauty@
@Description - English@
Sonya had a birthday recently. She was presented with the matrix of size n×m and consist of lowercase Latin letters. We assume that the rows are numbered by integers from 1 to n from bottom to top, and the columns are numbered from 1 to m from left to right.
Let’s call a submatrix (i1,j1,i2,j2) (1≤i1≤i2≤n;1≤j1≤j2≤m) elements aij of this matrix, such that i1≤i≤i2 and j1≤j≤j2. Sonya states that a submatrix is beautiful if we can independently reorder the characters in each row (not in column) so that all rows and columns of this submatrix form palidroms.
Let’s recall that a string is called palindrome if it reads the same from left to right and from right to left. For example, strings abacaba,bcaacb,a are palindromes while strings abca,acbba,ab are not.
Help Sonya to find the number of beautiful submatrixes. Submatrixes are different if there is an element that belongs to only one submatrix.
Input
The first line contains two integers n and m (1≤n,m≤250) — the matrix dimensions.
Each of the next n lines contains m lowercase Latin letters.
Output
Print one integer — the number of beautiful submatrixes.
Examples
input
1 3
aba
output
4
input
2 3
aca
aac
output
11
input
3 5
accac
aaaba
cccaa
output
43
Note
In the first example, the following submatrixes are beautiful: ((1,1),(1,1)); ((1,2),(1,2)); ((1,3),(1,3)); ((1,1),(1,3)).
In the second example, all submatrixes that consist of one element and the following are beautiful: ((1,1),(2,1)); ((1,1),(1,3)); ((2,1),(2,3)); ((1,1),(2,3)); ((2,1),(2,2)).
Some of the beautiful submatrixes are: ((1,1),(1,5)); ((1,2),(3,4)); ((1,1),(3,5)).
The submatrix ((1,1),(3,5)) is beautiful since it can be reordered as:
accca
aabaa
accca
In such a matrix every row and every column form palindromes.
@Translation@
给定一个 n*m 的,由小写字母组成的字符矩阵。
我们称一个矩阵是“beautiful”的,当且仅当将这个矩阵的每一行以某种顺序进行重排后,每一行是回文的,每一列也是回文的。
求矩阵有多少子矩阵是“beautiful”的。
@Solution@
首先我们要知道怎样的一个矩阵是“beautiful”的。
将每一行的字符个数统计出来,如果个数为奇数的字符 <= 1,则将这一行进行重排后可以得到回文串。
那么列呢?因为重排后每一列都是回文的,所以第一行的每个字符与最后一行的每一个字符是一一对应的。如果第一行中每种字符的出现次数等于最后一行每种字符的出现次数,那么我们总可以构造出这样一个矩阵使第一行等于最后一行,否则不可以构造。
因此,我们先枚举子矩阵的左右边界,求出每一行每种字符的出现次数,以及每一行字符个数为奇数的数量,并利用这些来求出合法的上下边界的数量。
怎么求解呢?首先这个上下边界间的行要满足个数为奇数的字符 <= 1。然后这个子矩阵的第一行中每种字符的出现次数等于最后一行每种字符的出现次数,第二行和倒数第二行,第三行和倒数第三行以此类推。
如果我们定义 “两行相等” 为 “两行每种字符的出现次数相等”,则相当于是要求回文串的个数。
这个其实就是 Manacher算法 的典型应用。
注意就是枚举左右边界时可以利用上一次的信息,以在较快的时间内求解出每一行的信息。
时间复杂度 O(26*n^3)。
@Code@
【所以我这个曾经一点都不会 Manacher 的人竟然去现学了这个算法然后竟然当场 A 了这道题???】
#include<cstdio>
const int MAXN = 250;
char s[MAXN + 5][MAXN + 5];
int a