POJ 2185 Milking Grid [二维KMP next数组]
直接转田神的了:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 6665 | Accepted: 2824 |
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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
Source
题目链接:http://poj.org/problem?id=2185
题目大意:给你一个r行c列的字符矩阵,令其一个子矩阵,使得这个子矩阵无限复制成的大矩阵包含原矩阵,现求这个子矩阵的最小尺寸
题目分析:1.把每行字符串看作一个整体对行求next数组
2.将矩阵转置
3.进行操作1,注意这里的行是原来的列,列是原来的行,相当于求原来列的next数组
4.求出len-next[len]即最小不重复子串的长度作为子矩形的边长
13892851 | njczy2010 | 2185 | Accepted | 42452K | 79MS | G++ | 1707B | 2015-02-16 10:51:05 |
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100
#define M 10005
//#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
char s[M][M];
char c[M][M];
int nexts[M];
int nextc[M];
int l;
int now; void get_nexts()
{
int i,j;
i=;j=-;nexts[]=-;
while(i<n)
{
if(j==- || strcmp(s[i],s[j])==){
i++;j++;nexts[i]=j;
}
else{
j=nexts[j];
}
}
} void get_nextc()
{
int i,j;
i=;j=-;nextc[]=-;
while(i<m)
{
if(j==- || strcmp(c[i],c[j])==){
i++;j++;nextc[i]=j;
}
else{
j=nextc[j];
}
}
} void ini()
{
int i,j;
for(i=;i<n;i++){
scanf("%s",s[i]);
}
for(j=;j<m;j++){
for(i=;i<n;i++){
c[j][i]=s[i][j];
}
c[j][i]='\0';
}
} void solve()
{
get_nexts();
get_nextc();
} void out()
{
printf("%d\n",(n-nexts[n])*(m-nextc[m]));
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}
POJ 2185 Milking Grid [二维KMP next数组]的更多相关文章
- Match:Milking Grid(二维KMP算法)(POJ 2185)
奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上 ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- POJ 2155 Matrix (二维树状数组)题解
思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
随机推荐
- H5新特性:
新增选择器 document.querySelector.document.querySelectorAll 拖拽释放(Drag and drop) API 媒体播放的 video 和 audio 本 ...
- centos下nginx安装和配置
注:此文是根据前辈的博客和自己实际动手总结出来的,不喜勿喷 1.准备工作 Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,注意安装顺序如下: 1 SSL功能需要 ...
- 破解点触码的识别之第三方平台超级鹰的SDK(python3版本)
import requestsfrom hashlib import md5 class Chaojiying(object): def __init__(self, username, passwo ...
- opencast 视频捕获代理 pyCA安装和功能实现
pyCA安装过程: 36 git clone https://github.com/opencast/pyCA.git 37 cd pyCA/ 41 yum install python-pycurl ...
- 暴力解说之首次部署NGINX
前言 本章基于Centos 7.x系统讲解 本章讲解下在项目上线部署的时候对NGINX的操作.有些童鞋在网上百度类似LNMP安装就跟着命令一条一条执行了,如果没报错还好,一旦报错就懵逼状态了.这是对自 ...
- shell 中exec、source以及bash的区别
在bash shell中,source.exec以及sh都可以用来执行shell script,但是它们的差别在哪里呢? sh:父进程会fork一个子进程,shell script在子进程中执行 so ...
- LeetCode(121) Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- Spring核心技术(十一)——基于Java的容器配置(一)
基本概念: @Bean和@Configuration Spring中新的基于Java的配置的核心就是支持@Configuration注解的类以及@Bean注解的方法. @Bean注解用来表示一个方法会 ...
- Python3中super()的参数传递
1. super([type[, object-or-type]]) super() 在使用时至少传递一个参数,且这个参数必须是一个类. 通过super()获取到的是一个代理对象,通过这个对象去查找父 ...
- charles-修改发送的接口数据测试页面样式
一.痛点: 1. 界面上数据准确性无法比对 2. 界面上几乎没有可测试数据 3. 消息条数超过99时的显示逻辑验证(难道真的要造100条新的未读消息?) 4. 更换界面图片时必须找相关接 ...