【KMP】POJ 2185

题目貌似有点难理解,忍不住看了一下解题报告。。。囧

方法:对每行求最小重复长度,求出lcm,记为ans1;再对每列求最小重复长度,求出lcm,记为ans2

ans1*ans2就是最后答案

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define N 1000005
char str[10005][80];
int x[80],y[10005];
int main () {
    int r,c;
    while (scanf("%d%d",&r,&c) != -1) {
        int i,j,k;
        for (i = 1; i <= r; i++) {
            scanf("%s",str[i] + 1);
        }
        int xx = 1;
        for (i = 1; i <= r; i++) {
            x[1] = 0;
            k = 0;
            for (j = 2; j <= c; j++) {
                while (k > 0 && str[i][k+1] != str[i][j]) {
                    k = x[k];
                }
                if (str[i][k+1] == str[i][j]) k++;
                x[j] = k;
            }
            int m = c - x[c];cout<<x[c]<<endl;
            xx = xx / abs(__gcd(xx,m)) * m;
        }
        int yy = 1;
        for (i = 1; i <= c; i++) {
            y[1] = 0;
            k = 0;
            for (j = 2; j <= r; j++) {
                while (k > 0 && str[k+1][i] != str[j][i]) {
                    k = y[k];
                }
                if (str[k+1][i] == str[j][i]) k++;
                y[j] = k;
            }
            int m = r - y[r];
            yy = yy / abs(__gcd(yy,m)) * m;
        }
        if (xx > c) xx = c;
        if (yy > r) yy = r;
        printf("%d\n",xx  * yy);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值