KMP矩阵匹配poj2185

Language:
Milking Grid
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6291 Accepted: 2629

Description

Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quite the expert on cow behavior, and is currently writing a book about feeding behavior in cows. He notices that if each cow is labeled with an uppercase letter indicating its breed, the two-dimensional pattern formed by his cows during milking sometimes seems to be made from smaller repeating rectangular patterns.  

Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.  

Input

* Line 1: Two space-separated integers: R and C  

* Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow's breed. Each of the R input lines has C characters with no space or other intervening character.  

Output

* Line 1: The area of the smallest unit from which the grid is formed  

Sample Input

2 5
ABABA
ABABA

Sample Output

2
一种方法是,KMP找出每一行每一列的周期,然后求行和列的LCM,作为矩形的长和宽

#include<iostream>
#include<stdio.h>
using namespace std;
char s[10005][80];
int subrow[10005];
int subcol[80];
int next[10005];
int row,col;
int nextrow(int r)
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<col)
    {
        if(j==-1||s[r][i]==s[r][j])
        {
            i++;j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
    return i-next[i];//这里我还是没弄懂会有最小覆盖于最大重复字串的和为i;
}
int nextcol(int c)
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<row)
    {
        if(j==-1||s[i][c]==s[j][c])
        {
            i++;j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
    return i-next[i];
}

int LCM(int a,int b)
{
    int x=a,y=b;
    int r=x%y;
    while(r>0)
    {
        x=y;
        y=r;
        r=x%y;
    }
    return a*b/y;
}
int main()
{
    while(scanf("%d%d",&row,&col)==2)
    {
        for(int i=0;i<row;i++)
            scanf("%s",s[i]);
        for(int i=0;i<row;i++)
            subrow[i]=nextrow(i);
        for(int i=0;i<col;i++)
            subcol[i]=nextcol(i);
        int x=1;
        for(int i=0;i<row;i++)
            x=LCM(x,subrow[i]);
        if(x>col)
            x=col;
        int y=1;
        for(int i=0;i<col;i++)
            y=LCM(y,subcol[i]);
        if(y>row)
            y=row;
        printf("%d\n",x*y);
    }
    return 0;
}

但这样好像对于有的数据是不能得到正确答案的,只能说数据有些水

可以先找出每一行中可能的长度都求出来,然后找出共同的且最小的最为举行的宽,然后把每一行看成一个字母,进行KMP,求得行数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
char s[10010][80];
int next[10010];
int main()
{
    int i,j,x,y,r,c,f[80];
    char a[80];
    scanf("%d%d",&r,&c);
    for(i=0;i<c;i++)f[i]=0;
    for(i=0;i<r;i++){
        scanf("%s",s[i]);
        strcpy(a,s[i]);
        //将每行的每种重复子串长度都求出来
        for(j=c-1;j>0;j--){
            a[j]=0;
            for(x=0,y=0;s[i][y];x++,y++){
                if(!a[x])x=0;
                if(a[x]!=s[i][y])break;
            }
            if(!s[i][y])f[j]++;
        }
    }
    for(i=0;i<c;i++)//找出所有行的最小相同的子串长度,为最小重复子矩阵的列数
        if(f[i]==r)break;
    x=i;//最小重复子矩阵的列数
    for(i=0;i<r;i++)s[i][x]=0;
    next[0]=-1;//按纵列求KMP的next函数,以求最小重复子矩阵的行数
    for(i=1,j=-1;i<r;i++){
        while(j!=-1&&strcmp(s[j+1],s[i]))j=next[j];
        if(!strcmp(s[j+1],s[i]))j++;
        next[i]=j;
    }
    printf("%d\n",(r-1-next[r-1])*x);//行列相乘即为最终结果
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值