标签:center 空格 ted apple string use put script blog
abc a 123456 45 abc ddd
1 4 -1
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int strlen(char *s) 7 { 8 int i=0; 9 while(s[i]!=‘\0‘) 10 { 11 i++; 12 } 13 return i; 14 } 15 16 void cnext(char *s,int *next) 17 { 18 int l=strlen(s); 19 int i=0,j=-1; 20 next[i]=j; 21 while(i<l-1) 22 { 23 if(j==-1||s[i]==s[j]) 24 { 25 i++; 26 j++; 27 next[i]=j; 28 } 29 else 30 { 31 j=next[j]; 32 } 33 } 34 } 35 36 int kmp(char *s1,char *s2,int *next) 37 { 38 int l1=strlen(s1),l2=strlen(s2); 39 int i=0,j=0; 40 while(i<l1&&j<l2) 41 { 42 if(j==-1||s1[i]==s2[j]) 43 { 44 i++; 45 j++; 46 } 47 else 48 { 49 j=next[j]; 50 } 51 } 52 if(j>=l2) 53 return i-l2+1; 54 return -1; 55 } 56 57 int main() 58 { 59 static char s1[1000010],s2[1000010]; 60 static int next[1000010]; //此处用static不完全等同于放到全局变量 61 while(gets(s1)) 62 { 63 gets(s2); 64 cnext(s2,next); 65 printf("%d\n",kmp(s1,s2,next)); 66 } 67 return 0; 68 } 69 70 /*************************************************** 71 User name: *** 72 Result: Accepted 73 Take time: 68ms 74 Take Memory: 1012KB 75 Submit time: 2016-11-02 21:33:59 76 ****************************************************/
标签:center 空格 ted apple string use put script blog
原文地址:http://www.cnblogs.com/Mimick/p/6031918.html