1415: Minimum coverage matrix
时间限制: 1 Sec 内存限制: 256 MB提交: 9 解决: 3
[ 提交][ 状态][ 讨论版]
题目描述
Give you a character matrix, you need to find a sub matrix which can “cover” the whole matrix, and its size should be minimum. In addition, you must assure that the left-top element is in the sub matrix. For example:
ABCDEFAB
AAAABAAA
ABCDEFAB
We can find the following minimum coverage matrix:
ABCDEF
AAAABA
Because by extension we can get:
ABCDEFABCDEF
AAAABAAAAABA
ABCDEFABCDEF
AAAABAAAAABA
We see it contains the original matrix.
输入
First line contains a integer T(1<=T<=30), indicating the case count.For each case, first line contains two integers R(1<=R<=500) and C(1<=C<=500), seperated by a space, there will be a R*C character matrix next.
输出
The size of minimum coverage matrix in a line.
样例输入
3
3 8
ABCDEFAB
AAAABAAA
ABCDEFAB
2 8
ABCDEFAB
AAAABAAA
1 1
A
样例输出
12
12
1
提示
来源
#include <cstring>
#include <algorithm>
using namespace std;
char s[550][550],s0[550][550];
int f[550],f0[550];
int m,n;
void getFail(char P[550][550],int f[550],int m){
f[0]=f[1]=0;
for (int i=1;i<m;i++){
int j=f[i];
while (j&&strcmp(P[i],P[j])!=0) j=f[j];
f[i+1]=strcmp(P[i],P[j])==0?j+1:0;
}
}
int main (){
int T;scanf("%d",&T);
while (T--){
scanf("%d%d",&n,&m);
memset(s0,0,sizeof(s0));
for (int i=0;i<n;i++) scanf("%s",s[i]);
for (int j=0;j<m;j++)
for (int i=0;i<n;i++){
s0[j][i]=s[i][j];
}
//printf("%d %d\n",n,m);
for (int j=0;j<m;j++) s0[j][n]=0;
//for (int j=0;j<m;j++)printf("%s\n",s0[j]);
getFail(s,f,n);
getFail(s0,f0,m);
printf("%d\n",(n-f[n])*(m-f0[m]));
}
return 0;
}