标签:
直接转田神的了:
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
13892851 | njczy2010 | 2185 | Accepted | 42452K | 79MS | G++ | 1707B | 2015-02-16 10:51:05 |
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 100 14 #define M 10005 15 //#define mod 10000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-6 21 //#define inf 2147483647 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 int n,m; 28 char s[M][M]; 29 char c[M][M]; 30 int nexts[M]; 31 int nextc[M]; 32 int l; 33 int now; 34 35 void get_nexts() 36 { 37 int i,j; 38 i=0;j=-1;nexts[0]=-1; 39 while(i<n) 40 { 41 if(j==-1 || strcmp(s[i],s[j])==0){ 42 i++;j++;nexts[i]=j; 43 } 44 else{ 45 j=nexts[j]; 46 } 47 } 48 } 49 50 void get_nextc() 51 { 52 int i,j; 53 i=0;j=-1;nextc[0]=-1; 54 while(i<m) 55 { 56 if(j==-1 || strcmp(c[i],c[j])==0){ 57 i++;j++;nextc[i]=j; 58 } 59 else{ 60 j=nextc[j]; 61 } 62 } 63 } 64 65 void ini() 66 { 67 int i,j; 68 for(i=0;i<n;i++){ 69 scanf("%s",s[i]); 70 } 71 for(j=0;j<m;j++){ 72 for(i=0;i<n;i++){ 73 c[j][i]=s[i][j]; 74 } 75 c[j][i]=‘\0‘; 76 } 77 } 78 79 void solve() 80 { 81 get_nexts(); 82 get_nextc(); 83 } 84 85 void out() 86 { 87 printf("%d\n",(n-nexts[n])*(m-nextc[m])); 88 } 89 90 int main() 91 { 92 //freopen("data.in","r",stdin); 93 //freopen("data.out","w",stdout); 94 //scanf("%d",&T); 95 //for(int ccnt=1;ccnt<=T;ccnt++) 96 //while(T--) 97 //scanf("%d%d",&n,&m); 98 while(scanf("%d%d",&n,&m)!=EOF) 99 { 100 ini(); 101 solve(); 102 out(); 103 } 104 return 0; 105 }
POJ 2185 Milking Grid [二维KMP next数组]
标签:
原文地址:http://www.cnblogs.com/njczy2010/p/4293817.html