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 ...
随机推荐
- Vue 导出表格为Excel
放法有多种,我这里是直接转JSON数据为Excel. 1.既然要使用,那首先当然是安装依赖,在终端命令输入: npm install -S file-saver xlsx npm install -D ...
- 决策树之C4.5算法学习
决策树<Decision Tree>是一种预測模型,它由决策节点,分支和叶节点三个部分组成. 决策节点代表一个样本測试,通常代表待分类样本的某个属性,在该属性上的不同測试结果代表一个分支: ...
- CSS控制显示超出部分,用省略号显示
经常使用.可是常忘,我又不是写css的.所以记下来: 先设置一下限制的宽度, display:block; white-space:nowrap; overflow:hidden; text-over ...
- alert警告框
标签中写: <div class="alert alert-warning fade in"> <button class="close" d ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- jQuery选择器,Ajax请求
jQuery选择器: $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $( ...
- Codeforces 919F. A Game With Numbers(博弈论)
Imagine that Alice is playing a card game with her friend Bob. They both have exactly 88 cards and ...
- Boost 解析xml——插入Item
XML格式为 <?xml version="1.0" encoding="utf-8"?> <Config> <Item name ...
- Windows系统 配置Java的JDK环境变量
安装了JDK或者绿色版后,在系统的环境变量设置中,进行以下配置: 1.新建->变量名"JAVA_HOME",变量值"D:\jdk1.8.0_05"(即JD ...
- [Node & Tests] Intergration tests for Authentication
For intergration tests, always remember when you create a 'mass' you should aslo clean up the 'mass' ...