Minimum coverage matrix(2013省赛第一题)

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

提示

来源


此题可以用kmp解决
              将每一列看做一个元素,按行kmp最大循环节k1
              将每一行看做一个元素,按列kmp最大循环节k2
    最后(n-k1)*(m-k2)即为所求。

我的代码:
 #include <cstdio>
#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值