前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)

POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)

作者头像
Gxjun
发布2018-03-22 13:56:28
5880
发布2018-03-22 13:56:28
举报
文章被收录于专栏:ml

Milking Grid

Time Limit: 3000MS

Memory Limit: 65536K

Total Submissions: 6169

Accepted: 2573

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

代码语言:javascript
复制
2 5
ABABA
ABABA

Sample Output

代码语言:javascript
复制
2

Hint

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

Source

USACO 2003 Fall

这道题,题目不是很好懂,首先是  

aabcdeaa

acbdeead   

dakfdkkk      ---》求最小的子矩阵 其实很简单的呀,对一行求出最小的循环节点  对每一列求出最小的循环节点就行了max={max, RR-next[RR]} 

dasdsdd         max1 ={max1,CC-next[CC]};  然后相乘得到了他的面积:   max1*max ==ans;

代码:

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 const int row =10050;
 8 const int cow =80;
 9 char str[row][cow];
10 int next[row][cow];
11 int RR,CC;
12 int main()
13 {
14   while(scanf("%d%d",&RR,&CC)!=EOF)
15   {
16       for(int i=0;i<RR;i++)
17           scanf("%s",str[i]);
18      int i,j,k;
19      //先求出每一行的next
20      int max_row=-1;
21      for(k=0;k<RR;k++)
22      {
23          i=0; j=-1;
24          next[k][0]=-1;
25          while(i<CC)
26          {
27              if(j==-1||str[k][i]==str[k][j])
28              {
29                  i++;
30                  j++;
31                  next[k][i]=j;
32              }
33              else j=next[k][j];
34          }
35          if(max_row<(CC-next[k][CC]))
36               max_row=(CC-next[k][CC]);
37      }
38      int max_cow=-1;
39      //求出所有列中的最小循环节
40      for(k=0;k<CC;k++)
41      {
42          i=0;
43          j=-1;
44          next[0][k]=-1;
45          while(i<RR)
46          {
47              if(j==-1||str[i][k]==str[j][k])
48              {
49                 i++;
50                 j++;
51                 next[i][k]=j;
52              }
53              else
54                  j=next[j][k];
55          }
56          if(max_cow<(RR-next[RR][k]))
57              max_cow=(RR-next[RR][k]);
58      }
59         printf("%d\n",max_row*max_cow);
60   }
61  return 0;
62 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档