POJ 2185 Milking Grid(KMP)

解题思路:

算是一个多维的KMP,其实是一样的,不过把1个字符的比较改成一行或一列字符的比较,对行和列使用两次KMP,最后乘起来就可以了。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 10000 + 10;
char s[maxn][100];
int rnext[maxn];
int cnext[maxn];
int R, C;
bool rsame(int x, int y)
{
    for(int i=0;i<C;i++)
    {
        if(s[x][i] != s[y][i])
            return 0;
    }
    return 1;
}
bool csame(int x, int y)
{
    for(int i=0;i<R;i++)
    {
        if(s[i][x] != s[i][y])
            return 0;
    }
    return 1;
}
int main()
{
    while(scanf("%d%d", &R, &C)!=EOF)
    {
        for(int i=0;i<R;i++)
            scanf("%s", s[i]);
        rnext[0] = 0, rnext[1] = 0;
        for(int i=1;i<C;i++)
        {
            int j = rnext[i];
            while(j && !csame(i,j)) j = rnext[j];
            rnext[i+1] = (csame(i,j)) ? j + 1 : 0;
        }
        /*for(int i=0;i<=C;i++)
            printf("%d ", rnext[i]);
        printf("\n");*/
        cnext[0] = 0, cnext[1] = 0;
        for(int i=1;i<R;i++)
        {
            int j = cnext[i];
            while(j && !rsame(i, j)) j = cnext[j];
            cnext[i+1] = (rsame(i, j)) ? j + 1 : 0;
        }
        /*for(int i=0;i<=R;i++)
            printf("%d ", cnext[i]);
        printf("\n");*/
        printf("%d\n", (R - cnext[R]) * (C - rnext[C]));
    }
}
时间: 2024-08-29 09:06:01

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

POJ 2185 Milking Grid (搬运KMP)

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

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

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; con

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 3450 Corporate Identity(KMP)

[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所有串做kmp,取匹配最小值的最大值就是答案. [代码] #include <cstring> #include <cstdio> #include <algorithm> const int N=4050,M=210; using namespace std; int nx

POJ 2406 Power Strings(kmp)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33335   Accepted: 13852 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

poj 3080 Blue Jeans(kmp)

Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tas