POJ-2185 Milking Grid
题目链接:POJ-2185
题目大意:找到一块字符串数组的最小子块
解题思路:将kmp变形 将每一列看作一个字符 求这一行字符串的next数组,将每一行看成一个字符 求这一列字符串的next数组,然后行数减去列的next数组最后一项就等于列的最小循环单位,行的最小循环单位同理,然后行列相乘就得到了面积
代码块:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string strA[10005];
int r, c;
int nextH[90], nextL[10005];
bool compareH(int x, int y){
for(int i = 0; i < c; i++){
if(strA[x][i] != strA[y][i]) return false;
}
return true;
}
bool compareL(int x, int y){
for(int i = 0; i < r; i++){
if(strA[i][x] != strA[i][y]) return false;
}
return true;
}
void getNextH(){
int i = 1, j = 0;
while(i < c){
if(compareL(i, j)){
nextH[i++] = ++j;
}else if(j != 0){
j = nextH[j - 1];
}else{
nextH[i++] = 0;
}
}
}
void getNextL(){
int i = 1, j = 0;
while(i < r){
if(compareH(i, j)){
nextL[i++] = ++j;
}else if(j != 0){
j = nextL[j - 1];
}else{
nextL[i++] = 0;
}
}
}
int main(){
cin>>r>>c;
for(int i = 0; i < r; i++){
cin>>strA[i];
}
getNextL();
getNextH();
cout<< (c - nextH[c - 1]) * (r - nextL[r - 1]);
return 0;
}