关闭

POJ 2185 (字符串最小覆盖)

标签: behaviorinputceachup
783人阅读 评论(0) 收藏 举报
分类:
Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3524   Accepted: 1446

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 

神题啊神题。。。太牛叉了

#include<stdio.h>
#include<string.h>

#define clear(a) memset(a,0,sizeof(a))

char st[10010][80],s_temp[10010];
int r,c,p[10010];

int in_data()
{
    int i;
    scanf("%d%d",&r,&c);
    for(i = 0;i < r;i++) scanf("%s",st[i]);
}

int gcd(int x,int y){
    if(y==0) return x;
    else return(gcd(y,x%y));
}

int lcm(int x,int y){
    return x*y/gcd(x,y);
}


int get_next(char s[],int len)
{
    int i,j,ans = len;
    clear(p);
    p[1] = 0;
    j = 0;
    for(i = 2;i <= len;i++) {
      while ((j > 0) && (s[j+1] != s[i]))
        j = p[j];
      if (s[j+1] == s[i])
        j++;
      p[i] = j;
      }
   // printf("%d\n",ans);
    return len - p[len];
}

int work()
{
   int i,j,lcm_r = 1,lcm_c = 1;
   for(i = 1;i <= r;i++) {
     for(j = 1;j <= c;j++)
       s_temp[j] = st[i-1][j-1];
       s_temp[c+1] = '\0';
     lcm_r = lcm(lcm_r,get_next(s_temp,c));
     if (lcm_r >= c) {lcm_r = c;break;}
     }
   for(j = 1;j <= c;j++) {
     for(i = 1;i <= r;i++)
       s_temp[i] = st[i-1][j-1];
       s_temp[r+1] = '\0';
     lcm_c = lcm(lcm_c,get_next(s_temp,r));
     if (lcm_c >= r) {lcm_c = r;break;}
     }
    //if (lcm_r > c) lcm_r = c;
    //if (lcm_c > r) lcm_c = r;
    return lcm_r * lcm_c;
}

int main()
{
    in_data();
    printf("%d\n",work());
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44668次
    • 积分:805
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    文章分类
    最新评论