Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 7153   Accepted: 3047

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'.
 
题意:找出矩阵中最小的子矩阵,满足循环它能形成包括原矩阵的矩阵
题解:
       对于每一行,当作一个字符,跑kmp,若有hang[n]!=0 则有循环节,则行最短循环节长度=小矩阵的高=n-hang[n]
    对于列也是如此
//作者:1085422276
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
//#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = ;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==)
{
x=;
y=;
return a;
}
p=exgcd(b,a%b,x,y);
temp=x;
x=y;
y=temp-(a/b)*y;
return p;
}
//*******************************
char mp[][];
int n,m,p[];
int is_equal(int i,int j)
{
for(int k=;k<m;k++)
{
if(mp[i][k]!=mp[j][k])return ;
}
return ;
}
int is_equaf(int i,int j)
{
for(int k=;k<n;k++)
{
if(mp[k][i]!=mp[k][j])return ;
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(p,,sizeof(p));
for(int i=;i<n;i++)
{
scanf("%s",mp[i]);
}
// for(int i=1;i<=n;i++)cout<<mp[i]<<endl;
//return 0;
int j=;
for(int i=;i<m;i++)
{
while(j&&!is_equaf(i,j))j=p[j];
if(is_equaf(i,j))j++;
p[i+]=j;
}
int l;
l=m-(p[m]);
memset(p,,sizeof(p));
j=;
for(int i=;i<n;i++)
{
while(j&&!is_equal(i,j))j=p[j];
if(is_equal(i,j))j++;
p[i+]=j;
}
int r;
r=n-(p[n]);
cout<<l*r<<endl;
}
return ;
}

代码

POJ 2185 Milking Grid KMP(矩阵循环节)的更多相关文章

  1. POJ 2185 Milking Grid KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  2. POJ 2185 Milking Grid [KMP]

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8226   Accepted: 3549 Desc ...

  3. [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...

  4. POJ 2185 Milking Grid(KMP最小循环节)

    http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...

  5. POJ 2185 Milking Grid [二维KMP next数组]

    传送门 直接转田神的了: Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accept ...

  6. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

  7. 题解报告:poj 2185 Milking Grid(二维kmp)

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  8. poj 2185 Milking Grid

    Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS   Memory Limit: 65536K       Descript ...

  9. Poj 2165 Milking Grid(kmp)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

随机推荐

  1. Mongodb for C# 分组查询

    #region 排序获取集合 static List<BsonDocument> GetPagerWithGroup(string connectionString, string dat ...

  2. CNN 美国有线电视新闻网 wapCNN WAP 指无线应用通讯协议 ---- 美国有线电视新闻网 的无线应用

    wapCNN  wap指无线应用通讯协议  CNN美国有线电视新闻网   固, wapCNN 美国有线电视新闻网的无线应用 -------------------------------------- ...

  3. motto6

    当你发现自己不错的时候,要适当的给自己加点凉水,让自己知道自己离“不错”还很远.

  4. codeforces 258div2 A Game With Sticks(DP)

    题目链接:http://codeforces.com/contest/451/problem/A 解题报告:有n跟红色的棍子横着放,m根蓝色的棍子竖着放,它们形成n*m个交点,两个人轮流在里面选择交点 ...

  5. sql大全

    推荐一. 简单查询   简单的Transact-SQL查询只包括选择列表.FROM子句和Where子句.它们分别说明所查询列.查询的表或视图.以及搜索条件等. 例如,下面的语句查询testtable表 ...

  6. mac os 安装 pkg-config

    wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.tar.gz . env LDFLAGS="-framework ...

  7. error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:值“0”不匹配值“2”

    error: vtkCommon.lib(vtkSmartPointerBase.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:值“0”不 ...

  8. facedetect

    继续学习大神的博文http://www.cnblogs.com/tornadomeet/archive/2012/03/22/2411318.html

  9. 最近win7更新后出现第二次打开IDE(delphi2007)的时候提示无法打开"EditorLineEnds.ttr"这个文件

    kb2982791 - 2014年8月12日更新 - http://support.microsoft.com/kb/2982791kb2970228 - 2014年8月12日更新 - http:// ...

  10. 使用nginx的proxy_cache做网站缓存

    为什么要做web cache,我想大家最主要的是解决流量的压力.随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力.与此同时某些网站的页面 ...