POJ:2185-Milking Grid(KMP找矩阵循环节)
Milking Grid
Time Limit: 3000MS
Memory Limit: 65536K
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.
OutputLine 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找循环节,然后求最小公倍数,然后WA,闷了好久才反应过来为啥只在行里面找,每一列也需要用KMP找寻环节得到列的最小公倍数,然后行列的最小公倍数乘起来才是最小循环矩阵的面积啊。但是要注意如果行或者列的最小公倍数比给出的n,m更大的时候要选择n,m,就是以自身为一个循环节
- 注意行列的大小,开数组的时候不要开反了,RE到怀疑人生。
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn_length = 1e4+100;
const int maxn_wide = 100;
char maps[maxn_length][maxn_wide];
ll n,m,next[maxn_length];
ll __gcd(ll a,ll b)
{
if(b == 0)
return a;
return __gcd(b,a%b);
}
ll kmp_len(ll pos)//每一行看毛片
{
ll k = -1;
memset(next,-1,sizeof(next));
for(int i=1;i<m;i++)
{
while(k > -1 && maps[pos][i] != maps[pos][k+1])
k = next[k];
if(maps[pos][i] == maps[pos][k+1])
k++;
next[i] = k;
}
return m-1-next[m-1];
}
ll get_lcm_length()//得到每一行的最小公倍数
{
ll ans = 1,cnt;
for(int i=0;i<n;i++)
{
ll len = kmp_len(i);
cnt = ans/__gcd(len,ans)*len;
ans = cnt;
if(ans > m)
return m;
}
return ans;
}
ll kmp_wide(ll pos)//每一列看毛片
{
ll k = -1;
memset(next,-1,sizeof(next));
for(int i=1;i<n;i++)
{
while(k > -1 && maps[i][pos] != maps[k+1][pos])
k = next[k];
if(maps[i][pos] == maps[k+1][pos])
k++;
next[i] = k;
}
return n-1-next[n-1];
}
ll get_lcm_wide()//得到每一列的最小公倍数
{
ll ans = 1,cnt;
for(int i=0;i<m;i++)
{
ll wide = kmp_wide(i);
cnt = ans/__gcd(ans,wide)*wide;
ans = cnt;
if(ans > n)
return n;
}
return ans;
}
int main()
{
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
scanf("%s",maps[i]);
ll lcm_len = get_lcm_length();
ll lcm_wide = get_lcm_wide();
lcm_len = min(lcm_len,(ll)m);
lcm_wide = min(lcm_wide,(ll)n);
ll ans = lcm_len * lcm_wide;
printf("%lld\n",ans);
}
return 0;
}
POJ:2185-Milking Grid(KMP找矩阵循环节)的更多相关文章
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- POJ 2185 Milking Grid [KMP]
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- POJ 2185 Milking Grid(KMP最小循环节)
http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...
- 题解报告:poj 2185 Milking Grid(二维kmp)
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
- Poj 2165 Milking Grid(kmp)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...
随机推荐
- 内核的执行头程序head.S
功能 定义data段和text段 重新手动初始化gdt表, idt表, tss表结构 初始化页表和页目录 --> 页目录的数据放在一个页表中 在页目录中, 其实地址为0x1000, 初始化页目录 ...
- sqlserver2008执行200M以上的大脚本文件,打开脚本总是报“未能完成操作,存储空间不足”
用sqlcmd命令行工具. 1.win7下快捷键:win+R 2.输入cmd,确定 3.输入命令:sqlcmd -S <数据库> -i C:\<数据文件>.sql 例:sql ...
- iis mvc html
iis mvc项目显示view文件夹下的html <system.webServer><handlers> <add name="JavaScriptHandl ...
- python复数
复数的概念在很久以前,数学家们被下面的等式困扰.x2=-1这是因为任何实数(无论正负)乘以自己总会得到一个非负数.一个数怎么可以乘以自己得到一负数?没有这样的实数存在.就这样18世纪,数学家们发了一个 ...
- vim 粘贴复制操作
原文链接:http://www.cnblogs.com/lansh/archive/2010/08/19/1803378.html vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十 ...
- EEC 欧姆龙PLC输入模块算法
Option Explicit Public MyArray(20000) As Integer Public MyArraySensor(20000) As Integer Sub 生成输入 ...
- IOS autosizing(设置控件的固定位置大小)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- Android(java)学习笔记80:Html嵌入到Java显示乱码
1. Html嵌入到Java显示乱码: 解决方案: 使用 loadData方法是中文部分会出现乱码,即使指定“utf-8”.“gbk”.“gb2312”也一样. webView.getSettings ...
- systemd初始化进程(转)
Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载内核→内核的初始化→启动初始化进程,初始化进程作为系统第一个进程,它需要 ...
- angular路由学习笔记
文章目录 标签routerLink路由传递参数 url中get传值 定义路由 获取参数 配置动态路由 定义路由 获取参数 API js路由跳转 配置动态路由 定义路由 获取参数 get传值 定义路由 ...