| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 6665 | Accepted: 2824 |
Description
Input
Output
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
Source
#include <cstdio>
#include <cstring>
char s[10005][80], rs[80][10005];
int R[10005], C[10005];
int r, c;
void get_nextR()
{
R[0] = -1;
int j = -1, i = 0;
while(i < r)
{
if(j == -1 || strcmp(s[i], s[j]) == 0)
{
i++;
j++;
R[i] = j;
}
else
j = R[j];
}
}
void get_nextC()
{
C[0] = -1;
int j = -1, i = 0;
while(i < c)
{
if(j == -1 || strcmp(rs[i], rs[j]) == 0)
{
i++;
j++;
C[i] = j;
}
else
j = C[j];
}
}
int main()
{
while(scanf("%d %d", &r, &c) != EOF)
{
for(int i = 0; i < r; i++)
scanf("%s", s[i]);
get_nextR();
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
rs[j][i] = s[i][j];
get_nextC();
printf("%d\n", (r - R[r]) * (c - C[c]));
}
}
POJ 2185 Milking Grid (二维KMP next数组)
原文地址:http://blog.csdn.net/tc_to_top/article/details/43746885