题意:每一行,每一列都不相同的矩形个数.(并不是矩阵里元素都不相同);
分析:
由于字符填充的种类比较少,暴力的话要:
52∗52∗n∗m
52
∗
52
∗
n
∗
m
.可以预处理每一个位置的最右端距离和最下端距离,然后DFS扫(注意扫的过程,并不是52*52),就优化掉一个52;但是注意看我code里的样例,那个是过不去的,要对于每一个位置二次预处理,才能正解;
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define met(s) memset(s, 0, sizeof(s))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 2e3 + 10;
char str[MAXN][MAXN];
LL dp1[MAXN][MAXN], dp2[MAXN][MAXN];
bool vis[200];
LL ans = 0;
int n, m;
inline void dfs(int x, int y, LL lenx, LL leny) {
if(leny <= 0 || lenx + x - 1 > n) return;
else if(dp1[x + lenx - 1][y] >= leny && dp2[x][y + leny - 1] >= lenx) {
ans += leny;
dfs(x, y, lenx + 1, leny);
} else {
dfs(x, y, lenx, leny - 1);
}
}
int main() {
scanf("%d %d", &n, &m);
rep(i, 1, n) scanf("%s", str[i] + 1);
rep(i, 1, n) {
rep(j, 1, m) {
met(vis);
rep(p, 0, 55) {
if(vis[str[i][j + p] - 'A'] || j + p > m) {
dp1[i][j] = p * 1LL;
break;
}
vis[str[i][j + p] - 'A'] = true;
}
met(vis);
rep(p, 0, 55) {
if(vis[str[i + p][j] - 'A'] || i + p > n) {
dp2[i][j] = p * 1LL;
break;
}
vis[str[i + p][j] - 'A'] = true;
}
}
}
LL arr[60];
rep(i, 1, n) {
rep(j, 1, m) {
rep(p, 1, 55) {
arr[p] = dp2[i][j + p];
dp2[i][j + p] = min(dp2[i][j + p], dp2[i][j + p - 1]);
}
dfs(i, j, 1, dp1[i][j]);
rep(p, 1, 55) {
dp2[i][j + p] = arr[p];
}
}
}
printf("%I64d\n", ans);
return 0;
}
/*
2 3
asd
bsc
*/