poj 2185

 不会,题意还是不理解,我感觉选的这个矩形没有一点要按同一个规则摆放吧。 如果是这样真心感觉做不出来。

如果不是这样就是简单的kmp。  

我是为了练习扩展kmp才来看这题的,结果,。。。貌似扩展kmp也可以做的样子. 但是kmp的做法更好理解吧

 

Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4859   Accepted: 2020

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

Hint

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

Source

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff

int n,m;

char g[10010][80];

int next[10010];

int cmp(char tmp[],char tmp1[])
{
    for(int i=0;i<m;i++)
        if(tmp[i]!=tmp1[i]) return 0;
    return 1;
}

int cmp1(int x,int y)
{
    for(int i=0;i<n;i++)
    {
        if(g[i][x]!=g[i][y]) return 0;
    }
    return 1;
}

int main()
{
    //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
    //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",g[i]);
    int j=0;
    next[0]=n;
    next[1]=0;
    for(int i=1;i<n;i++)
    {
        while(j!=0 && cmp(g[i],g[j])==0) j=next[j];
        if(cmp(g[i],g[j])==1) j++;
        next[i+1]=j;
    }
    int ans=n-next[n];
    j=0;
    next[0]=m;
    next[1]=0;
    for(int i=1;i<m;i++)
    {
        while(j!=0&&cmp1(j,i)==0) j=next[j];
        if(cmp1(j,i)==1) j++;
        next[i+1]=j;
    }
    printf("%d\n",ans*(m-next[m]));
    return 0;
}

 

posted @ 2013-07-28 16:19  chenhuan001  阅读(262)  评论(0编辑  收藏  举报