| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 3524 | Accepted: 1446 |
Description
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
* 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
#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;
}
852




被折叠的 条评论
为什么被折叠?



