Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10084   Accepted: 4371

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

题意:

找一个最小循环子矩阵。最后一个循环节可以是不完整的。

思路:

【看题的时候完全没思路】

首先我们可以找到一行的所有循环节,一行的最小循环节不一定是最小循环矩阵的行数,因为也许之后有的行时不以这个循环的。S[1~i]的最小循环节就是S[1~i-nxt[i]], S[1~i - nxt[nxt[i]]]是次小循环节,以此类推。我们统计一下每一行循环节的长度,当某个长度k出现的次数为r时,说明每一行都以[1~k]为循环,并且要找到最小的k。

接着我们求列数。将一行的[1~k]作为整体,使用strcmp进行KMP匹配。可以找到列的最小循环节,即列数。

相乘就是答案。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int r, c;
const int maxn = 1e5 + ;
char s[maxn][];
int nxtrow[maxn][], cntrow[maxn], nxtcol[maxn]; int main()
{
while(scanf("%d%d", &r, &c) != EOF){
memset(cntrow, , sizeof(cntrow));
for(int i = ; i <= r; i++){
scanf("%s", s[i] + );
nxtrow[i][] = ;
for(int j = , k = ; j <= c; j++){
while(k > && s[i][j] != s[i][k + ])k = nxtrow[i][k];
if(s[i][j] == s[i][k + ])k++;
nxtrow[i][j] = k;
//cout<<j<<" "<<nxtrow[i][j]<<endl;
}
for(int j = c; j > ; j = nxtrow[i][j]){
cntrow[c - nxtrow[i][j]]++;
}
}
int row;
for(int i = ; i <= c; i++){
if(cntrow[i] == r){
row = i;
break;
}
}
//cout<<row<<endl;
for(int i = ; i <= r; i++){
s[i][row + ] = ;
} //nxtcol[1] = 0;
for(int i = , j = ; i <= r; i++){
while(j > && strcmp(s[i] + , s[j + ] + ) != )j = nxtcol[j];
if(strcmp(s[i] + , s[j + ] + ) == )j++;
nxtcol[i] = j;
//cout<<i<<" "<<nxtcol[i]<<endl;
} //cout<<nxtcol[r]<<endl;
printf("%d\n", (r - nxtcol[r]) * row);
}
return ;
}

poj2185 Milking Grid【KMP】的更多相关文章

  1. POJ2185 Milking Grid 【lcm】【KMP】

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

  2. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  3. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  4. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  5. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  6. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  8. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  9. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

随机推荐

  1. 【转】【Java/Android】Intent的简介以及属性的详解

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  2. Android ListView使用BaseAdapter与ListView的优化 (转至 http://www.open-open.com/lib/view/open1339485728006.html)

    在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点.读者可 ...

  3. erlang-百度云推送Android服务端功能实现-erlang

    百度云推送官方地址http://developer.baidu.com/wiki/index.php?title=docs/cplat/push 简单的介绍下原理: 百度云推送支持IOS和Androi ...

  4. PHP curl_setopt函数用法介绍补充篇

    1.curl数据采集系列之单页面采集函数get_html 单页面采集在数据采集过程中是最常用的一个功能 有时在服务器访问限制的情况下 只能使用这种采集方式 慢 但是可以简单的控制 所以写好一个常用的c ...

  5. Linux配置防火墙,开启80port、3306port 可能会遇到的小问题

     vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(同意80端口通 ...

  6. C# GetType和typeof的区别

    typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...

  7. 微信jssdk批量添加卡券接口(踩坑经验)

    1)首先是官方接口文档: 1.批量添加卡券接口:https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.0861973 ...

  8. ftp简单命令

    1.连接ftp ftp 192.168.10.15 进去后输入用户名 ,然后再输入密码,就这样登陆成功了,你会看到 ftp> 2.进入ftp后,你对目录需要切换操作.和linux一样的命令.cd ...

  9. Android的读写文件及权限设置

    drwx read write excute openFileOutput(name,drwx); 用系统api读取文件 设置文件生成的权限:    public static boolean sav ...

  10. iOS 9 分屏多任务:入门(中文版)

    本文转载至 http://www.cocoachina.com/ios/20150714/12555.html 本文由钢铁侠般的卿哥(微博)翻译自苹果官方文档:Adopting Multitaskin ...