标签:
Description
Input
Output
Sample Input
2 5 ABABA ABABA
Sample Output
2
Hint
Source
1 /* 2 唐代李白 3 《登金陵凤凰台》 4 5 凤凰台上凤凰游,凤去台空江自流。 6 吴宫花草埋幽径,晋代衣冠成古丘。 7 三山半落青天外,二水中分白鹭洲。 8 总为浮云能蔽日,长安不见使人愁 9 */ 10 #include <iostream> 11 #include <cstdio> 12 #include <algorithm> 13 #include <cstring> 14 #include <vector> 15 #include <utility> 16 #include <iomanip> 17 #include <string> 18 #include <cmath> 19 #include <queue> 20 #include <assert.h> 21 #include <map> 22 #include <ctime> 23 #include <cstdlib> 24 #include <stack> 25 #define LOCAL 26 const int MAXN = 10000 + 10; 27 const int MAXM = 75 + 10; 28 const int INF = 100000000; 29 const int SIZE = 450; 30 const int maxnode = 0x7fffffff + 10; 31 using namespace std; 32 int l1, l2; 33 int next[MAXN];//不用开太大了.. 34 int Ans[MAXN]; 35 char data[MAXN][MAXM]; 36 int r, c;//长和宽 37 38 /*void getNext(){ 39 next[1] = 0; 40 int j = 0; 41 for (int i = 2; i <= n; i++){ 42 while (next[j] > 0 && strcmp(data[j + 1] + 1, data[i] + 1)) j = next[j]; 43 if (!strcmp(data[j + 1] + 1, data[i] + 1)) j++; 44 next[i] = j; 45 } 46 return; 47 }*/ 48 //a是模板链, b是匹配串 49 /*int kmp(char *a, char *b){ 50 int j = 0, cnt = 0; 51 for (int i = 1; i <= l2; i++){ 52 while (next[j] > 0 && a[j + 1] == b[i]) j = next[j]; 53 if (a[j + 1] == b[i]) j++; 54 if (j == m) return 1;//? 55 } 56 }*/ 57 58 /*void init(){ 59 scanf("%s", a + 1); 60 scanf("%s", b + 1); 61 l1 = strlen(a + 1); 62 l2 = strlen(b + 1); 63 }*/ 64 int cnt[MAXM], h[MAXN]; 65 char a[MAXM]; 66 67 void init(){ 68 scanf("%d%d", &r, &c); 69 memset(cnt, 0, sizeof(cnt)); 70 for (int i = 0 ; i < r; i++){ 71 scanf("%s", data[i]); 72 strcpy(a, data[i]); 73 for(int j = c - 1; j > 0; j--){ 74 a[j]=0; 75 int x = 0, y; 76 for(y = 0; data[i][y] ; y++){ 77 if(!a[x]) x = 0; 78 if(a[x] != data[i][y]) break; 79 x++; 80 } 81 if( !data[i][y] ) cnt[j]++; 82 } 83 } 84 } 85 void work(){ 86 int i; 87 for(i = 0; i < c; i++) if(cnt[i] == r)break; 88 int x = i; 89 for(int i = 0;i < r; i++) data[i][x] = 0; 90 next[0] = -1;//按纵列求KMP的next函数,以求最小重复子矩阵的行数 91 for(int i = 1, j = -1; i < r; i++){ 92 while(j != -1 && strcmp(data[j+1],data[i])) j = next[j]; 93 if(!strcmp(data[j+1], data[i])) j++; 94 next[i] = j; 95 } 96 printf("%d\n",(r - 1 - next[r-1]) * x);//行列相乘即为最终结果 97 } 98 99 100 int main(){ 101 int T; 102 103 init(); 104 work(); 105 106 return 0; 107 }
【POJ2185】【KMP + HASH】Milking Grid
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4333891.html