Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8325   Accepted: 3588

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

___________________________________________________________________________________________________________________

题目大意:给定矩形字符矩阵,问它是多大的子矩阵铺成的。

每一行(或列)组成的字符串,最后一个字母的next[]为它的前缀,那么“最后一个字母坐标-next[最后一个字母]”就是当前行(或列)铺设的字符串的长度(标记为L)。那么所有行(或列)的L的最小公倍数就是最终子矩阵的长(或宽)。长宽相乘就是结果。需要注意的是当子矩阵长(或宽)大于矩阵本身的长(或宽)时,则直接赋值为矩阵本身的长(或宽),也就是说结果最大也就是矩阵本身。

___________________________________________________________________________________________________________________

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4
5 using namespace std;
6
7 int r,c;
8 int rg=1,cg=1;
9 char str[10010][77];
10 char s[10010];
11 int next[10010];
12
13 void getnext(int c)
14 {
15 next[0]=-1;
16 for(int j,i=1;i<c;i++)
17 {
18 j=next[i-1];
19 while(s[j+1]!=s[i] && j>=0)j=next[j];
20 next[i]=s[j+1]==s[i]?j+1:-1;
21 }
22 }
23 int gys(int a,int b)
24 {
25 if(a%b==0)return b;
26 else return gys(b,a%b);
27 }
28 int gbs(int a,int b)
29 {
30 return a/gys(a,b)*b;
31 }
32 int main()
33 {
34 scanf("%d%d",&r,&c);
35 for(int i=0;i<r;i++)
36 {
37 scanf("%s",str[i]);
38 strcpy(s,str[i]);
39 getnext(c);
40 rg=gbs(rg,c-next[c-1]-1);
41 if(rg>c)rg=c;
42 }
43 s[r]=0;
44 for(int i=0;i<c;i++)
45 {
46 for(int j=0;j<r;j++)s[j]=str[j][i];
47 getnext(r);
48 cg=gbs(cg,r-next[r-1]-1);
49 if(cg>r)cg=r;
50 }
51 printf("%d",rg*cg);
52 return 0;
53 }

poj2185Milking Grid的更多相关文章

  1. POJ2185-Milking Grid(KMP,next数组的应用)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6317   Accepted: 2648 Desc ...

  2. POJ2185Milking Grid(最小覆盖子串 + 二维KMP)

    题意: 一个r*c的矩形,求一个子矩形通过平移复制能覆盖整个矩形 关于一个字符串的最小覆盖子串可以看这里http://blog.csdn.net/fjsd155/article/details/686 ...

  3. ExtJS 4.2 Grid组件的单元格合并

    ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...

  4. WPF中Grid实现网格,表格样式通用类

    /// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...

  5. 在 Windows Phone 中,为 Grid 添加 Tilt 效果

    在 Windows Phone 中,Tilt 效果是比较经典的效果,我们可以很简单的为按钮等控件添加这样的效果(使用 Windows Phone Toolkit 的Tilt 效果),但是,如果我们想要 ...

  6. wpf 列表、菜单 收起与展开,通过Grid DoubleAnimation或者Expander实现

    菜单收缩有很多种方法具体如何实现还是看个人想法: 第一种通过后台控制收起与展开: 效果图: 代码 : <Grid> <Grid.ColumnDefinitions> <C ...

  7. Sencha ExtJS 6 Widget Grid 入门

    最近由于业务需要,研究了一下Sencha ExtJS 6 ,虽然UI和性能上据相关资料说都有提升,但是用起来确实不太顺手,而且用Sencha cmd工具进行测试和发布,很多内部细节都是隐藏的,出了问题 ...

  8. WPF CheckBox样式 ScrollViewer样式 WrapPanel、StackPanel、Grid布局

    本节讲述布局,顺带加点样式给大家看看~单纯学布局,肯定是枯燥的~哈哈 那如上界面,该如何设计呢? 1.一些布局元素经常用到.Grid StackPanel Canvas WrapPanel等.如上这种 ...

  9. [转]ExtJS Grid 分页时保持选中的简单实现方法

    原文地址 :http://www.qeefee.com/article/ext-grid-keep-paging-selection ExtJS中经常要用到分页和选择,但是当选择遇到分页的时候,杯具就 ...

随机推荐

  1. bean中属性名和json不一致解决方案(请求和响应)

    此时@RequestBody.@ResponseBody需要与@JsonProperty结合使用,才能做到请求正常解析,响应按要求格式返回. 注意@JsonProperty注解的位置需要加在gette ...

  2. [leetcode]62.UniquePaths

    /** * Created by lvhao on 2017/7/6. * A robot is located at the top-left corner of a m x n grid (mar ...

  3. [leetcode]110BalancedBinaryTree平衡二叉树

    public boolean isBalanced(TreeNode root) { int res = helper(root); if (res<0) return false; retur ...

  4. 嵌入式Linux-Bmp转jpeg

    代码: /**************************************************************************** Ãû³Æ£º jpeg.c ¹ŠÄÜ ...

  5. JavaDailyReports10_09

    ***************************** 1.2.2 布局管理器 BorderLayout 把容器的布局分为东西南北中五个部位,默认是中间,平铺占满! 1 package awt; ...

  6. Docker环境下升级PostgreSQL

    查阅PostgreSQL官方文档可以得知,官方提供了两种方式对数据库进行升级--pg_dumpall与pg_upgrade. pg_dumpall是将数据库转储成一个脚本文件,然后在新版数据库中可以直 ...

  7. Asp.Net Core 应用配置

    五种读取方式 五种读取方式依赖于 IConfiguration 和 IConfigurationRoot 对象 一.初级写法 //不区分大小写 string connectionString = _c ...

  8. 手撕redis分布式锁,隔壁张小帅都看懂了!

    前言 上一篇老猫和小伙伴们分享了为什么要使用分布式锁以及分布式锁的实现思路原理,目前我们主要采用第三方的组件作为分布式锁的工具.上一篇运用了Mysql中的select ...for update实现了 ...

  9. PHP jquer网页打印插件 PrintArea

    <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...

  10. ThreadX应用笔记:内核初始化和任务调度

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 了解ThreadX的初始化流程有助于移植使用,掌握任务的的调度有助于更加得心应手地运用该实时操作系统. 二.初始化 ...