POJ 2185 Milking Grid KMP循环节周期
题目来源: id=2185" target="_blank">POJ 2185 Milking Grid
题意:至少要多少大的子矩阵 能够覆盖全图
比如例子 能够用一个AB 组成一个
ABABAB
ABABAB 能够多出来
思路:每一行求出周期 总共n个 求这n个周期的最小公倍数 假设大于m 取m
每一列求出周期 总共m个求这个m个周期的最小公倍数 假设大于n取n
答案就是2个最小公倍数的积
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 10010;
char a[maxn][77];
char b[77][maxn];
int f[maxn][77];
int f2[77][maxn];
int gcd(int a, int b)
{
return b?gcd(b, a%b):a;
}
void getFail(char* p, int* f)
{
int m = strlen(p);
f[0] = f[1] = 0;
for(int i = 1; i < m; i++)
{
int j = f[i];
while(j && p[i] != p[j])
j = f[j];
f[i+1] = p[i] == p[j] ? j+1 : 0;
}
} int main()
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
{
scanf("%s", a[i]);
getFail(a[i], f[i]);
}
for(int i = 0; i < m; i++)
{
for(int j = 1; j <= n; j++)
{
b[i+1][j-1] = a[j][i];
}
b[i+1][n] = 0;
}
for(int i = 1; i <= m; i++)
getFail(b[i], f2[i]);
int ans1 = 1, ans2 = 1;
for(int i = 1; i <= n; i++)
{
ans1 = ans1/gcd(ans1, m-f[i][m])*(m-f[i][m]);
if(ans1 > m)
{
ans1 = m;
break;
}
}
for(int i = 1; i <= m; i++)
{
ans2 = ans2/gcd(ans2, n-f2[i][n])*(n-f2[i][n]);
if(ans2 > n)
{
ans2 = n;
break;
}
}
printf("%d\n", ans1*ans2);
return 0;
}
POJ 2185 Milking Grid KMP循环节周期的更多相关文章
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ 2185 Milking Grid [KMP]
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- POJ 2185 Milking Grid(KMP最小循环节)
http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- 题解报告:poj 2185 Milking Grid(二维kmp)
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
- poj 2406 Power Srings (kmp循环节) (经典)
<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...
随机推荐
- Linux之lldptool命令
1. 描述 当我们想在操作系统里面查看网口和交换机连接的状态信息,我们可以使用lldptool这个工具. 2.LLDP协议 LLDP是一个数据链路层发现协议,LLDP协议使得接入网络的一台设备可以将其 ...
- [TypeScript] Shallow copy object by using spread opreator
For example we have an object: const todo = { text: "Water the flowers", completed: false, ...
- 源代码编译安装CloudStack 4.2
基于CentOS 6.4安装CloudStack 环境配置 # yum -y update # yum -y upgrade 安装NTP,jdk 1.7, tomcat 6, mysql,git等服务 ...
- 制作 wordpress 博客静态化到本地
wget 克隆 wordpress 博客镜像 wget -e robots=off -w 1 -xq -np -nH -pk -m -t 1 -P "./wordpress.org" ...
- CISP/CISA 每日一题 11
CISA 每日一题(答) 一个合理建造的数据仓库应当支持下列三种基本的查询格式: 1.向上溯源和向下溯源——向上溯源是对数据进行总计:向下溯源是将数据进行细化: 2.交叉溯源——通过通用属性访问数据仓 ...
- CentOS不能进入登录界面
http://blog.csdn.net/powerzone/article/details/6798646
- 洛谷 P2819 图的m着色问题
P2819 图的m着色问题 题目背景 给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的.图的 ...
- [React] Render Text Only Components in React 16
In this session we create a comment component to explore how to create components that only render t ...
- 有趣的Ruby-学习笔记4
Ruby块 块.在我看来就是插入一段可变的函数 block_name{ statement1 statement2 .......... } 看起来不知道是什么,只是别急,继续往下看. 块函数通过yi ...
- php函数symlink详解
php函数symlink详解 建立符号链接 (symbolic link),类似于Windows里头常用的.lnk快捷方式 symlink语法: int symlink(string target, ...