poj2185 Milking Grid【KMP】
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10084 | Accepted: 4371 |
Description
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
* 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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
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】的更多相关文章
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
随机推荐
- 基于mvcpager的分页(get请求,刷新页面),提供两种样式(来自bootstrap的样式)
使用方法:先把mvcpager.dll引用加入mvc项目 下载路径在本文末尾 前台代码 前台: @{ Layout = null; } @using Webdiyer.WebControls.Mvc ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)
接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...
- c# T obj = default(T);
泛型类和泛型方法同时具备可重用性.类型安全和效率,这是非泛型类和非泛型方法无法具备的.泛型通常用在集合和在集合上运行的方法中..NET Framework 2.0 版类库提供一个新的命名空间 Syst ...
- PL/SQL如何调试Oracle存储过程
from:http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非常重要的,Oracle作 ...
- webpack2.0简单配置教程
以前习惯用gulp+less来开发项目,由于公司项目用的vue开发的,所以学下webpack这个打包工具.以下是我学习时的笔记,希望给在webpack配置过程中遇到麻烦的朋友一丝帮助. 目前只配置了s ...
- 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...
- Linux ulimit 命令
ulimit命令用来限制系统用户对 shell 资源的访问,常见用法如下: [root@localhost ~]$ ulimit -a # 查看当前所有的资源限制 [root@localhost ~] ...
- Qt监控excel
配置文件setup.ini内容 [General] ExcelFilePath=D:/项目资料/GSC-西门子开关/GSCOPC.xlsx GameIp=192.168.1.152 GamePort= ...
- UISearchBar和UISearchDisplayController
原文 http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8 UISearchBar继承自UIView.UIResponder ...
- js中replace()方法
str.replace(/Microsoft/g, "W3School");//全局替换 str.replace(/Microsoft/, "W3School" ...