首页 > 代码库 > POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)

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

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

2 5ABABAABABA

Sample Output

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;
 
 
代码:
 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      //先求出每一行的next20      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              else54                  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 }
View Code