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
    评论
KMP算法(Knuth-Morris-Pratt算法)是一种用于解决字符串匹配问题的高效算法。它的主要思想是利用匹配失败时的信息,尽量减少比较次数,提高匹配效率。 KMP算法的核心是构建一个部分匹配表(Partial Match Table),也称为Next数组。这个表记录了在匹配失败时应该将模式串向右移动的位置。 构建部分匹配表的过程如下: 1. 首先,将模式串中的第一个字符的Next值设为0,表示当匹配失败时,模式串不需要移动; 2. 然后,从模式串的第二个字符开始,依次计算Next值; 3. 当第i个字符与前面某个字符相同的时候,Next[i]的值为该字符之前(不包括该字符)的相同前缀和后缀的最大长度; 4. 如果不存在相同的前缀和后缀,则Next[i]的值为0。 有了部分匹配表之后,KMP算法的匹配过程如下: 1. 用i和j来分别表示模式串和主串的当前位置; 2. 如果模式串中的字符和主串中的字符相同,那么i和j都向右移动一位; 3. 如果模式串中的字符和主串中的字符不同,那么根据部分匹配表来确定模式串的下一个位置; 4. 假设当前模式串的位置为i,根据部分匹配表中的值Next[i],将模式串向右移动Next[i]个位置; 5. 重复上述步骤,直到找到匹配或者主串遍历完毕。 KMP算法的时间复杂度为O(m + n),其中m和n分别是模式串和主串的长度。相比于暴力匹配算法的时间复杂度为O(m * n),KMP算法能够大幅减少比较次数,提高匹配效率。 综上所述,KMP模式匹配算法通过构建部分匹配表并利用匹配失败时的信息,实现了高效的字符串匹配。在实际应用中,KMP算法被广泛地应用于文本编辑、数据搜索和字符串处理等领域。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值