↓ Skip to main content
  1. Posts/

poj 2185 Milking Grid (最小覆盖子矩形,kmp)

·436 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

poj 2185 题目链接

题意:给出一个字符矩形,问一个面积最小的矩形,覆盖掉整个矩形。大概就是二维的最小覆盖子串。

思路:对于每一行做最小覆盖子串,然后求lcm,每一列也是如此。最后记得判断不能超过原有的n,m。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月10日 星期三 23时46分47秒
 4File Name :code/poj/2185.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <stack>
14#include <set>
15#include <map>
16#include <string>
17#include <cmath>
18#include <cstdlib>
19#include <deque>
20#include <ctime>
21#define fst first
22#define sec second
23#define lson l,m,rt<<1
24#define rson m+1,r,rt<<1|1
25#define ms(a,x) memset(a,x,sizeof(a))
26typedef long long LL;
27#define pi pair < int ,int >
28#define MP make_pair
29
30using namespace std;
31const double eps = 1E-8;
32const int dx4[4]={1,0,0,-1};
33const int dy4[4]={0,-1,1,0};
34const int inf = 0x3f3f3f3f;
35const int N=1E4+7;
36char s[N][80];
37int n,m;
38int nxt[N];
39void getrownxt(int row,int n)
40{
41    int i = 0 ;
42    int j = -1;
43    nxt[0] = -1;
44    while (i<n)
45	if (j==-1||s[row][i]==s[row][j]) nxt[++i]=++j;
46	else j = nxt[j];
47}
48void getcolnxt(int col,int n)
49{
50    int i = 0 ;
51    int j = -1;
52    nxt[0] = -1;
53    while (i<n)
54	if (j==-1||s[i][col]==s[j][col]) nxt[++i]=++j;
55	else j = nxt[j];
56}
57int gcd( int a,int b)
58{
59    if (a%b==0) return b;
60    return gcd(b,a%b);
61}
62int lcm(int a,int b)
63{
64    return a/gcd(a,b)*b;  //蒟蒻的自我修养
65}
66int main()
67{
68	#ifndef  ONLINE_JUDGE
69	freopen("code/in.txt","r",stdin);
70  #endif
71
72	cin>>n>>m;
73	for ( int i = 0 ; i < n ; i++) scanf("%s",s[i]);
74
75	int L = 1;
76	for ( int i =  0 ; i < n ; i++)
77	{
78	    getrownxt(i,m);
79	    L = lcm(L,m-nxt[m]);
80	}
81	int R = 1;
82	for ( int i = 0 ; i < m ; i++)
83	{
84	    getcolnxt(i,n);
85	    R = lcm(R,n-nxt[n]);
86	}
87
88//	cout<<"L:"<<L<<" R:"<<R<<endl;
89	L = min(L,m);
90	R = min(R,n);
91	printf("%d\n",L*R);
92
93
94
95  #ifndef ONLINE_JUDGE
96  fclose(stdin);
97  #endif
98    return 0;
99}

Related

KMP与最小覆盖子串

·1041 words·3 mins
参考资料(本文大部分是参考这篇博客,附带一些证明步骤的解释) 首先明确一些概念: 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串。 比如: 对于s=“abcab”,它的最小覆盖子串p=“abc”,因为p通过在它后面再接上一个p(即重叠0个字符),可以得到q=“abcabc”,此时s是q的子串。 对于s=“ababab”,它的最小覆盖子串为p=“ab”。

poj 2752 Seek the Name, Seek the Fame (kmp 理解nxt函数)

·298 words·1 min
poj 2752题目链接 题意:求出所有的前缀和后缀相同的子串的长度。 思路:求出nxt函数,观察发现,从从len递归向前就是答案。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月10日 星期三 21时05分52秒 4File Name :code/poj/2752.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <deque> 20#include <ctime> 21#define fst first 22#define sec second 23#define lson l,m,rt<<1 24#define rson m+1,r,rt<<1|1 25#define ms(a,x) memset(a,x,sizeof(a)) 26typedef long long LL; 27#define pi pair < int ,int > 28#define MP make_pair 29 30using namespace std; 31const double eps = 1E-8; 32const int dx4[4]={1,0,0,-1}; 33const int dy4[4]={0,-1,1,0}; 34const int inf = 0x3f3f3f3f; 35const int N=4E6+7; 36int n; 37string a; 38int nxt[N]; 39void getnxt( int n) 40{ 41 int i = 0 ; 42 int j = -1 ; 43 nxt[0] = -1; 44 while (i<n) 45 if (j==-1||a[i]==a[j]) nxt[++i]=++j; 46 else j = nxt[j]; 47} 48void print( int x) 49{ 50 if (nxt[x]!=-1) 51 { 52 print(nxt[x]); 53 printf("%d ",x); 54 } 55} 56int main() 57{ 58 #ifndef ONLINE_JUDGE 59 freopen("code/in.txt","r",stdin); 60 #endif 61// ios::sync_with_stdio(false); 62 while (cin>>a) 63 { 64 int len = a.length(); 65 getnxt(len); 66 // for ( int i = 0 ; i < len ; i++) cout<<i<<" nxt[i]:"<<nxt[i]<<endl; 67 print(nxt[len]); 68 printf("%d\n",len); 69 70 } 71 72 #ifndef ONLINE_JUDGE 73 fclose(stdin); 74 #endif 75 return 0; 76}

hdu 1686 Oulipo (kmp模板题)

·473 words·1 min
hdu1686 题意:给出模式串和文本串,问模式串在文本串中出现了多少次,可以overlap. 思路:思考naive的匹配过程。nxt函数不过是改进了当失配发生时,不是移动1位,而是移动多位。nxt函数的含义是当失配发生时,移动到的位置….所以有的教程管这个叫失配函数吧,也不是很难理解的样子。

KMP算法学习

·943 words·2 mins
20170801update:当时竟然没有强调 next 函数的含义? next[i] 的含义是,i 之前的整个前缀中,最长的「前缀和后缀相同」的长度。 看图: KMP 感觉是我学到现在最难懂的一个算法了 QAQ 为什么你们都那么强啊,看几个小时就看懂了……

poj 2406 Power Strings (后缀数组||kmp)

·1756 words·4 mins
poj 2406 题意:给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值 思路:论文题。 转载论文中的题解: 做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足。判断的时候, 先看字符串 L 的长度能否被 k 整除,再看 suffix(1) 和 suffix(k+1) 的最长公共前缀是否等于 n-k。在询问最长公共前缀的时候,suffix(1) 是固定的,所以 RMQ 问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到 height[rank[1]] 之间的最小值即可。整个做法的时间复杂度为 O(n)