poj 2185 Milking Grid(KMP)

题目链接:poj 2185 Milking Grid

题目大意:给定一个N?M的矩阵,找到一个最小的r?c的矩阵,使得原矩阵可以由若干个小矩阵组成,输出r?c的值。

解题思路:将行和列分别看成字符串,然后hash,对hash后的数组分别求KMP。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef unsigned long long ll;
const ll x = 123;
const int maxr = 10005;
const int maxc = 80;

int N, M, jump[maxr];
ll r[maxr], c[maxc];
char s[maxr][maxc];

void init () {
    for (int i = 1; i <= N; i++)
        scanf("%s", s[i] + 1);
    memset(r, 0, sizeof(r));
    memset(c, 0, sizeof(c));

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++)
            r[i] = r[i] * x + s[i][j];
    }

    for (int j = 1; j <= M; j++) {
        for (int i = 1; i <= N; i++)
            c[j] = c[j] * x + s[i][j];
    }
}

int kmp (int n, ll* a) {
    int p = 0;
    for (int i = 2; i <= n; i++) {
        while(p && a[p+1] != a[i])
            p = jump[p];

        if (a[p+1] == a[i])
            p++;
        jump[i] = p;
    }
    return n - jump[n];
}

int solve () {
    return kmp(N, r) * kmp(M, c);
}

int main () {
    while (scanf("%d%d", &N, &M) == 2) {
        init();
        printf("%d\n", solve());
    }
    return 0;
}
时间: 2024-10-27 04:12:23

poj 2185 Milking Grid(KMP)的相关文章

POJ 2185 Milking Grid KMP循环节周期

题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一个AB 组成一个 ABABAB ABABAB 能够多出来 思路:每一行求出周期 总共n个 求这n个周期的最小公倍数 假设大于m 取m 每一列求出周期 总共m个求这个m个周期的最小公倍数 假设大于n取n 答案就是2个最小公倍数的积 #include <cstdio> #include <cst

POJ 2185 Milking Grid (二维KMP next数组)

Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accepted: 2824 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

POJ 2185 Milking Grid

Milking Grid Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 218564-bit integer IO format: %lld      Java class name: Main Every morning when they are milked, the Farmer John's cows form a rectangular grid th

POJ 2185 Milking Grid(KMP)

解题思路: 算是一个多维的KMP,其实是一样的,不过把1个字符的比较改成一行或一列字符的比较,对行和列使用两次KMP,最后乘起来就可以了. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include &l

POJ 2185 Milking Grid (搬运KMP)

题意:给你一个字母矩阵,让你找一个最小的字母矩阵,复制后可以得到大的矩阵,问最小矩阵面积是多少,假设原来的矩阵长度是6,你用一个长度为5的串复制,多余的部分不算,只算6以内相同即可 思路:其实没有什么思路,看了一些大佬们的博客,其中有几篇写的很棒 传送门1,传送门2 大体的思路就是找到每行的循环节,然后插入在桶中,在另一侧记录的时候发现等于列的数量是就代表这是最小的(细节部分窝还要想几天) 代码: 不存在的(最近搬运代码越来越熟练了) 原文地址:https://www.cnblogs.com/l

POJ 2185 二维KMP

题意:就是让你求出最小的字符矩阵面积,这个矩阵是这个大矩阵里能够循环的,但是并不一定是全部循环相同的,部分相同也算循环,比如样例. 思路:这题挺好的,以前没有想到二维字符串数组也可以用next数组求出其循环节,现在这题正好补了这个空. 解法:把每一个字符串当做字符进行求next数组,然后求出最小的循环字符串长度,即:len-next[len].因为以前求循环节是len/(len-next[len]),括号里面的不就是最小的循环长度嘛! 因为要求这个循环矩阵的长和宽,所以长就是每一行作为一个字符串

POJ2185 Milking Grid(KMP)

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

POJ 2185 Milking Grid--另一种字符串的循环节

在Power String中,求一个字符串的循环节,应满足L mod (L-next[L])=0,则循环节长度为L-next[L] 存在另一种形式的循环节,例如abcabca,此时如果将abc重写三次,得到abcabcabc,则原字符串为其前缀. 此时对于原字符串,其循环节长度为L-next[L]=7-4=3,循环节为abc.具体可见下面这个题 [Baltic2009]Radio Transmission 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知

poj2185 Milking Grid(KMP运用)

题目链接:http://poj.org/problem?id=2185 Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6249   Accepted: 2616 Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 1