poj2185 Milking Grid

Milking Grid

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3879 Accepted: 1598

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

USACO 2003 Fall

字符串的好题
咋一看摸不到头绪,但是自习想会有一些想法
可以求出每一行的覆盖的,和每一列的覆盖的
然后再求最小公倍数之类的处理
我们可以完善一下思路,看那个discuss里有个讲的好的
http://blog.sina.com.cn/s/blog_69c3f0410100tyjl.html
链接到这了

找出每行的重复子串长度的各种可能情况,然后每行都有的并且是最小长度作为宽width。
第二步找最小重复子矩阵的高,这个思路和网上的差不多,取每行的宽为width的前缀作为一个单位,对这0到r-1个单位求出KMP的next函数,找出最小重复子序列的单位数作为高height,最终答案为width*height。

代码哎
涉及了好多东西,
kmp的next 的用法 请移步这里 http://blog.csdn.net/xiaoxiaoluo/article/details/7422912
这里证明了 一个字符串的最小覆盖子串是 len-next[len]
然后求那个width也有些技巧
代码很短,但很精彩

code
#include <cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<cmath>
#include 
<ctime>
#include 
<cassert>
#include 
<iostream>
#include 
<sstream>
#include 
<fstream>
#include 
<map>
#include 
<set>
#include 
<vector>
#include 
<queue>
#include 
<algorithm>
#include 
<iomanip>
using namespace std;
#define maxn 10005
char s[maxn][80];
int r,c;
int p[maxn],f[80];
char a[80];
int main()
{
    
int i,j,x,y;
    scanf(
"%d%d",&r,&c);
    memset(f,
0,sizeof(f));
    
for(i=0; i<r; i++)
    
{
        scanf(
"%s",s[i]);
        strcpy(a,s[i]);
        
for(j=c-1; j>0; j--)
        
{
            a[j]
='\0';//changduwwei j
            for(x=0,y=0; s[i][y]; x++,y++)
            
{
                
if(a[x]=='\0') x=0;//jieduan
                if(a[x]!=s[i][y])break;//butong bushi chongfuzichuan
            }

            
if(s[i][y]=='\0')f[j]++;//changduwei j keyi fugaiquanchuan
        }

    }

    
for(i=1; i<c; i++)
        
if(f[i]==r) break;//最短能覆盖的公共长度
    x=i;
    
//cout<<x<<endl;
    for(i=0; i<r; i++) s[i][x]='\0';
    p[
0]=-1;//kmp求next的过程
    j=-1;
    
for(i=1; i<r; i++)
    
{
        
while((j!=-1)&&strcmp(s[j+1],s[i])) j=p[j];
        
if(strcmp(s[j+1],s[i])==0) j++;
        p[i]
=j;
    }

    
//cout<<r-1-p[r-1]<<endl;
    printf("%d\n",(r-1-p[r-1])*x);
    
return 0;
}

posted on 2012-07-18 22:17 jh818012 阅读(139) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论