void readAll(FILE * f,unsigned char * c,unsigned char * zoek,int *v,int * aantal,int zoekl,char * filename){ int startindex=1,tekstl,i=0,end=0; //start=index van het eerste char dat in de buffer zit int n=BUFFER_SIZE; //het aantal in te lezen chars in 1 keer DEFAULT= 1000000 int rollover = zoekl-1; //moet de grootte van de zoekstring zijn -1 //eerste maal inlezen i= fread(c+i,1,BUFFER_SIZE,f); if(i==0) //lege file return; c[i] = 0; KMP(zoek,c,v,aantal,startindex,zoekl,i,filename); memmove(c,c+(n-rollover),rollover); //rollover vooraan zetten //lees BUFFER_SIZE characters in while(!feof(f)){ startindex=startindex+BUFFER_SIZE-rollover; //startindex updaten i = fread(c+rollover,1,BUFFER_SIZE-rollover,f); if(i== BUFFER_SIZE-rollover){ //niet op het einde c[BUFFER_SIZE]=0; tekstl=BUFFER_SIZE; } else{ c[i+rollover]=0; tekstl=i+rollover; } KMP(zoek,c,v,aantal,startindex,zoekl,tekstl,filename); memmove(c,c+(n-rollover),rollover); //rollover vooraan zetten } }
void patmatch ( int n , int m ) { char *S, *T1, *T2; int m1, m2, *match1, *match2; int i, j, k; S = randstr(n); m1 = m - 1; T1 = randstr(m1); m2 = m; T2 = randstr(m2); printf("S = %s\n", S); printf("T = %s*%s\n", T1, T2); match1 = KMP(S,T1,n,m1); match2 = KMP(S,T2,n,m2); printf("Pattern matches at index pairs"); for (i=j=1; i<=match1[0]; ++i) { k = match1[i] + m1; if (k > match2[match2[0]]) break; while ((j <= match2[0]) && (k > match2[j])) ++j; for (k=j; k<=match2[0]; ++k) printf(" (%d,%d)", match1[i], match2[k]); } printf("\n"); free(S); free(T1); free(T2); free(match1); free(match2); }
int main() { int x; char *A = "abababaababacbaaaababacb"; char *B = "ababacb"; char BB[N]; int next[N]; int i; memset(BB, 0, sizeof(BB)); BB[0] = '0'; for (i=0; i<strlen(B); i++) { BB[i+1] = B[i]; } printf("%s %s\n", A, BB); getNext(BB, strlen(B), next); i = KMP(A, strlen(A), 0, BB, strlen(B), next); printf("%d\n", i); getchar(); return 0; }
int main(int argc, char *argv[]) { std::string pattern = "abaabc", src = "acabaabaabcacaabc"; std::vector<int> pos = KMP(src, pattern); std::cout << pos << std::endl; std::string pattern1 = "aaaab", src1 = "aaabaaaab"; pos = KMP(src1, pattern1); std::cout << pos << std::endl; return 0; }
int main() { int n; scanf("%d\n",&n); while(n--) { memset(pat, '\0', sizeof(pat)); fgets(pat,10100,stdin); lenp = strlen(pat); if(pat[lenp-1]=='\n') { pat[lenp-1] = '\0'; lenp--; } memset(string, '\0', sizeof(string)); fgets(string,1000100,stdin); lens = strlen(string); if(string[lens-1]=='\n') { string[lens-1] = '\0'; lens--; } memset(f,0,sizeof(f)); failure(); printf("%d\n",KMP()); } return 0; }
/* ./exec filename keyword */ int main(int argc, char** argv) { if(argc!=3) { puts("Wrong param"); return -1; } /* open file */ int fd = open(argv[1], O_RDONLY); if(fd==-1) { puts("Fail to open file"); exit(EXIT_FAILURE); } struct stat sb; if(fstat(fd, &sb) == -1) { puts("fstat error"); exit(EXIT_FAILURE); } size_t len = sb.st_size; /* memory map */ char* mptr = (char*)mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); /* cal. fail function and count matching patterns */ fail(argv[2]); int result = KMP(mptr, len, argv[2]); printf("Result = %d\n", result); return 0; }
int main() { int t,n,i,j,k; char ans[MaxPL]; freopen("poj3080.txt","r",stdin); freopen("poj3080ans.txt","w",stdout); scanf("%d",&t); while (t--) { scanf("%d",&n); for (i=0;i<n;i++) scanf("%s",s[i]); ans[0]='\0'; for (i=60;i>=3;i--) if (i>=strlen(ans)) { for (j=0;j<=60-i;j++) { strcopy(s[0],pat,j,j+i-1); for (k=1;k<n;k++) { KMP(pat,s[k],res); if (res[0]==0) break; } if (k<n) continue; //当前子串不符合 if (strlen(ans)==0) strcpy(ans,pat); else if (strcmp(ans,pat)>0) strcpy(ans,pat); } } if (strlen(ans)==0) printf("no significant commonalities\n"); else printf("%s\n",ans); } return 0; }
int main() { int next[MAXSTRLEN+1]; int i,j,n; SString a,b; char op[100],op1[20]="Query",op2[20]="Push_back",op3[20]="Push_front",ch; //接下来初始化主串 ch=getchar(); for(i=1;i<=MAXSTRLEN&&(ch!='\n');i++) // 录入主串 { a[i]=ch; ch=getchar(); } a[0]=i-1; //接下来初始化模式串 ch=getchar(); for(i=1;i<=MAXSTRLEN&&(ch!='\n');i++) { b[i]=ch; ch=getchar(); } b[0]=i-1; // printf("op:\n"); scanf("%d",&n);getchar(); i=0; // while((ch=getchar())!='\n') op[i++]=ch; while(n--) { // printf("n==%d\n", n); // gets(op); i=0; while((ch=getchar())!='\n') op[i++]=ch; op[i]='\0'; if(strstr(op,op1)) { // printf("Query!\n"); KMP(a,b,next); } else if(strstr(op,op2)) { int len=b[0]; b[len+1]=op[10]; b[0]=len+1; // printf("Push_back-->%c\n",op[10] ); // for(i=1;i<=b[0];i++) printf("%c", b[i]);putchar('\n'); } else if(strstr(op,op3)) { int len=b[0],i; for(i=len;i>=1;i--) b[i+1]=b[i]; b[1]=op[11]; b[0]=len+1; // printf("Push_front--->%c",op[11]); // for(i=1;i<=b[0];i++) printf("%c", b[i]);putchar('\n'); } // while((ch=getchar())!='\n') op[i++]=ch; } return 0; }
int main() { scanf("%s%s", pattern, text); printf("%d\n", KMP(pattern, text)); return 0; }
int main( int argc, char **argv ) { char *text = "grtgrtg34r4tgrtgrtgeg54tg54t54tgtrgrtALAr43r34vfgvrt56uh76"; char *find = "ALA"; KMP( find, 3, text, strlen( text ) ); return 0; }
char *strStr(char *haystack, char *needle) { int target_index = KMP(haystack, needle); if(target_index == -1) { return NULL; } else { return haystack+target_index; } }
int main() { std::string s1, s2; std::getline(std::cin, s1); std::getline(std::cin, s2); KMP(s1, s2); return 0; }
int main() { char text[MAXN],pattern[MAXN]; int t,n; scanf("%s",pattern); scanf("%s",text); KMP(text,pattern); return 0; }
int main() { scanf("%d",&caseNum); while(caseNum--) { scanf("%s%s",mod+1,s+1); len1=strlen(s+1); len2=strlen(mod+1); printf("%d\n",KMP()); } return 0; }
int strStr(char *haystack, char *needle) { if (!haystack || !needle) return 0; int l_h = strlen(haystack); int l_n = strlen(needle); if(l_n == 0) return 0; if(l_h == 0) return -1; int pos = KMP(haystack, needle); return pos; }
int main() { freopen("hdoj2087.txt","r",stdin); freopen("hdoj2087ans.txt","w",stdout); while (scanf("%s",text)!=EOF) { if (text[0]=='#') break; scanf("%s",pat); KMP(pat,text,res); printf("%d\n",res[0]); } return 0; }
int main(int argc, char** argv) { if(argc!=3) { puts("Wrong param"); return -1; } fail(argv[2]); int result = KMP(argv[1], strlen(argv[1]), argv[2]); printf("Result = %d\n", result); return 0; }
int findLength(vector<int>& A, vector<int>& B) { vector<int> copy; int ans; copy = A; ans = 0; while (copy.size() > 0) { preKMP(copy); ans = max(ans, KMP(copy, B)); copy.erase(copy.begin()); } return ans; }
void strmatch ( int n, int m ) { char *S, *T; int *match, i; S = randstr(n); T = randstr(m); printf("S = %s\n", S); printf("T = %s\n", T); match = KMP(S,T,n,m); printf("%d matches found at indices", match[0]); for (i=1; i<=match[0]; ++i) printf(" %d", match[i]); printf("\n"); free(S); free(T); free(match); }
int main(void) { scanf("%d", &tests); for(int t = 0; t < tests; ++ t) { scanf("%s", word); rWord = std::string(word); std::reverse(rWord.begin(), rWord.end()); sWord = rWord + "$" + std::string(word); letters = KMP(sWord); printf("%s%s\n", word, rWord.c_str() + letters); } return 0; }
int main() { int n,t,i,ans; freopen("hdoj1711.txt","r",stdin); freopen("hdoj1711ans.txt","w",stdout); scanf("%d",&t); while (t--) { scanf("%d%d",&txtlen,&patlen); for (i=0;i<txtlen;i++) scanf("%d",&text[i]); for (i=0;i<patlen;i++) scanf("%d",&pat[i]); ans=KMP(); if (ans>=0) printf("%d\n",ans+1); else printf("-1\n"); } }
bool little::getID(char* str) { addrinfo hints; ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; addrinfo *pai; if (getaddrinfo("my.zi-jin.com", "80", &hints, &pai) != 0) Sleep(2000); SOCKET sck = socket(AF_INET, SOCK_STREAM, 0); if (INVALID_SOCKET == sck) return false; try { if (connect(sck, pai->ai_addr, pai->ai_addrlen) == SOCKET_ERROR) throw L""; const char strHeader[] = "GET "LittlePath"sign.php HTTP/1.1\r\nHost: my.zi-jin.com\r\nConnection: Close\r\n\r\n"; if (send(sck, strHeader, strlen(strHeader), 0) == SOCKET_ERROR) throw L""; } catch (...) { closesocket(sck); return false; } char buf[2048]; int received = recv(sck, buf, 2047, 0); buf[received] = '\0'; closesocket(sck); int i; i = KMP(buf, "\r\n\r\n", 0, true); if (i < 0) return false; while (buf[i++] != '['); strcpy(str, buf + i); i = -1; while (str[++i] != ']'); str[i] = '\0'; return true; }
int main() { while(scanf("%s",s)) { if(strlen(s)==1&&s[0]=='.')break; int t=KMP(); int n=strlen(s); if(n%(n-t-1)==0) { printf("%d\n",n/(n-t-1)); } else { printf("1\n"); } } // while(1); return 0; }
int repeatedStringMatch(string A, string B) { string str; int ans, k; preKMP(B); ans = 0; str = ""; k = 0; while (true) { ans++; str += A; if (KMP(B, str, k) == true) return ans; k = str.size() - B.size(); if (k < 0) k = 0; if (str.size() >= B.size() * 2) break; } return -1; }
int main() { int i,txtlen,patlen; freopen("hdoj2203.txt","r",stdin); freopen("hdoj2203ans.txt","w",stdout); while (scanf("%s",text)!=EOF) { scanf("%s",pat); txtlen=strlen(text); patlen=strlen(pat); if (txtlen<patlen) { printf("no\n"); continue; } for (i=0;i<patlen;i++) text[txtlen+i]=text[i]; txtlen+=patlen; text[txtlen]='\0'; KMP(pat,text,res); if (res[0]>0) printf("yes\n"); else printf("no\n"); } return 0; }
int cgi_extract_sections(output_buffer_function *writer) { int content_length; const char *p; char *boundary; int boundary_length; int match; output_section_t os; p = getenv("REQUEST_METHOD"); if (!p || strcmp(p, "POST") != 0) { syslog(LOG_WARNING, "cgi_filefetch not POST"); return(CGIPARSE_ERR_FORMAT); } p = getenv("CONTENT_LENGTH"); if (!p || ((content_length = atoi(p)) == 0)) { syslog(LOG_WARNING, "cgi_filefetch bad content length"); return(CGIPARSE_ERR_DATA); } p = getenv("CONTENT_TYPE"); if (!p || strncmp(p, MULTIPART_FORM_DATA, sizeof(MULTIPART_FORM_DATA) - 1) != 0) { syslog(LOG_WARNING, "cgi_filefetch not type: %s", MULTIPART_FORM_DATA); return(CGIPARSE_ERR_DATA); } /* Now search for boundary=XXX */ p = strstr(p, "boundary="); if (!p) { syslog(LOG_WARNING, "cgi_filefetch bad or missing boundary specification"); return(CGIPARSE_ERR_DATA); } p = strchr(p, '=') + 1; debug("Got boundary=[%s]\n", p); /* Now search for --<boundary> * Note that we don't search for \r\n--<boundary> since * sometimes?? the first \r\n is missing */ boundary_length = strlen(p) + 2; boundary = alloca(boundary_length + 1); sprintf(boundary, "--%s", p); os.timeout = 0; os.len = 0; os.pos = 0; os.consumed = 0; os.in_section = 0; os.writer = writer; set_nonblock(0); /* Now iterate through each item separated by the boundary */ while ((match = KMP(boundary, boundary_length, getter_section, &os)) >= 0) { debug("Found match at %d\n", match - boundary_length); /* Flush all the bytes up until the match. */ os.consumed = os.len - (os.pos - match); if (os.in_section) { /* We have been outputting this section. Back up by the boundary length * (plus 2 for the \r\n) and flush the buffer */ debug("reached end of section, match=%d, os.len=%d, os.pos=%d, boundary_length=%d\n", match, (int)os.len, (int)os.pos, boundary_length); assert(os.consumed >= boundary_length + 2); os.writer(os.name, os.content_type, os.buf, os.consumed - boundary_length - 2, os.pos - os.len); } os_discard(&os, os.consumed); os.consumed = 0; while (os.len < 2) if (os_read(&os) <= 0) goto err; char ch1 = os.buf[0]; char ch2 = os.buf[1]; os_discard(&os, 2); if (ch1 == '\r' && ch2 == '\n') { /* we are at a boundary, so process this section */ if (process_section(&os) <= 0) goto err; } else if (ch1 == '-' && ch2 == '-') { debug("This is the last section\n"); return CGIPARSE_ERR_NONE; } else { debug("Warning: Ignoring section with unknown terminator: '%c%c'\n", ch1, ch2); } os.pos = os.len; } err: if (os.timeout) { return CGIPARSE_ERR_TIMEDOUT; } else { return CGIPARSE_ERR_DATA; } }
bool DictBase::SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data) { int nWord = SearchWords.size(); std::vector<bool> WordFind(nWord, false); int nfound=0; if (dictfile) fseek(dictfile, idxitem_offset, SEEK_SET); if (dictfile) fread(origin_data, idxitem_size, 1, dictfile); else dictdzfile->read(origin_data, idxitem_offset, idxitem_size); gchar *p = origin_data; guint32 sec_size; int j; if (!sametypesequence.empty()) { gint sametypesequence_len = sametypesequence.length(); for (int i=0; i<sametypesequence_len-1; i++) { switch (sametypesequence[i]) { case 'm': case 't': case 'y': case 'l': case 'g': case 'x': case 'k': case 'w': case 'h': for (j=0; j<nWord; j++) // KMP() is faster than strstr() in theory. Really? Always be true? //if (!WordFind[j] && strstr(p, SearchWords[j].c_str())) { if (!WordFind[j] && KMP(p, strlen(p), SearchWords[j].c_str())!=-1) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; sec_size = strlen(p)+1; p+=sec_size; break; default: if (g_ascii_isupper(sametypesequence[i])) { sec_size = get_uint32(p); sec_size += sizeof(guint32); } else { sec_size = strlen(p)+1; } p+=sec_size; } } switch (sametypesequence[sametypesequence_len-1]) { case 'm': case 't': case 'y': case 'l': case 'g': case 'x': case 'k': case 'w': case 'h': sec_size = idxitem_size - (p-origin_data); for (j=0; j<nWord; j++) //if (!WordFind[j] && g_strstr_len(p, sec_size, SearchWords[j].c_str())) { if (!WordFind[j] && KMP(p, sec_size, SearchWords[j].c_str())!=-1) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; break; } } else { while (guint32(p - origin_data)<idxitem_size) { switch (*p) { case 'm': case 't': case 'y': case 'l': case 'g': case 'x': case 'k': case 'w': case 'h': for (j=0; j<nWord; j++) if (!WordFind[j] && strstr(p, SearchWords[j].c_str())) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; sec_size = strlen(p)+1; p+=sec_size; break; default: if (g_ascii_isupper(*p)) { sec_size = get_uint32(p); sec_size += sizeof(guint32); } else { sec_size = strlen(p)+1; } p+=sec_size; } } } return false; }
bool DictBase::SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data) { const int nWord = SearchWords.size(); std::vector<bool> WordFind(nWord, false); int nfound=0; if (dictfile) fseek(dictfile, idxitem_offset, SEEK_SET); if (dictfile) { size_t fread_size; fread_size = fread(origin_data, idxitem_size, 1, dictfile); if (fread_size != 1) { g_print("fread error!\n"); } } else { dictdzfile->read(origin_data, idxitem_offset, idxitem_size); } gchar *p = origin_data; guint32 sec_size; int j; if (!sametypesequence.empty()) { const gint sametypesequence_len = sametypesequence.length(); for (int i=0; i<sametypesequence_len-1; i++) { if(is_dict_data_type_search_data(sametypesequence[i])) { sec_size = strlen(p); for (j=0; j<nWord; j++) // KMP() is faster than strstr() in theory. Really? Always be true? if (!WordFind[j] && KMP(p, sec_size, SearchWords[j].c_str())!=-1) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; sec_size += sizeof(gchar); p+=sec_size; } else { if (g_ascii_isupper(sametypesequence[i])) { sec_size = g_ntohl(get_uint32(p)); sec_size += sizeof(guint32); } else { sec_size = strlen(p)+1; } p+=sec_size; } } if(is_dict_data_type_search_data(sametypesequence[sametypesequence_len-1])) { sec_size = idxitem_size - (p-origin_data); for (j=0; j<nWord; j++) if (!WordFind[j] && KMP(p, sec_size, SearchWords[j].c_str())!=-1) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; } } else { while (guint32(p - origin_data)<idxitem_size) { if(is_dict_data_type_search_data(*p)) { for (j=0; j<nWord; j++) if (!WordFind[j] && strstr(p, SearchWords[j].c_str())) { WordFind[j] = true; ++nfound; } if (nfound==nWord) return true; sec_size = strlen(p)+1; p+=sec_size; } else { if (g_ascii_isupper(*p)) { sec_size = g_ntohl(get_uint32(p)); sec_size += sizeof(guint32); } else { sec_size = strlen(p)+1; } p+=sec_size; } } } return false; }
/* ./exec filename keyword proc_num */ int main(int argc, char** argv) { if(argc!=4) { puts("Wrong param"); return -1; } /* open file */ int fd = open(argv[1], O_RDONLY); if(fd==-1) { puts("Fail to open file"); exit(EXIT_FAILURE); } /* get file size */ struct stat sb; if(fstat(fd, &sb) == -1) { puts("fstat error"); exit(EXIT_FAILURE); } size_t len = sb.st_size-1; /* memory map */ char* mptr = (char*)mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); if(mptr == MAP_FAILED) { perror("mmap failed"); exit(EXIT_FAILURE); } /* segment info. */ int proc_num = atoi(argv[3]); size_t avg_len = len/proc_num; size_t remain = len%proc_num; size_t key_len = strlen(argv[2]); size_t normal_seg_len = avg_len+key_len-1; size_t tail_seg_len = avg_len+remain; /* Check prcesses and length */ if(avg_len<key_len) { puts("Too many processes or key length is greater than that of the string"); exit(EXIT_FAILURE); } /* Calculate the fail function */ fail(argv[2]); /* Create shared memory to save result */ int rfd = shm_open("match_num", OPEN_FLAG, OPEN_MODE); if(rfd == -1) { puts("shm_open failed"); exit(EXIT_FAILURE); } if(ftruncate(rfd, FILE_SIZE) == -1) { puts("ftruncate failed"); exit(EXIT_FAILURE); } int* rptr = (int*)mmap(NULL, FILE_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, rfd, 0); if(rptr == MAP_FAILED) { puts("Result mmap error"); exit(EXIT_FAILURE); } /* fork n processes */ for(int i=0; i<proc_num; i++) { pid_t pid = fork(); if(pid==0) { /* Set info for normal and tail segments */ size_t seg_len; bool istail; if(i==proc_num-1) { seg_len = tail_seg_len; istail = true; } else { seg_len = normal_seg_len; istail = false; } // Use shared memory to get the result from child processes. int result = *(rptr+i) = KMP(mptr+i*avg_len, seg_len, argv[2], istail); // DEBUG - CHECK SEGMENT /* char check[100]; strncpy(check, mptr+i*avg_len, seg_len); check[seg_len] = '\0'; printf("This is child %d, result = %d, word = %s, seg_len = %zu\n", i, result, check, seg_len); */ exit(EXIT_SUCCESS); } } /* wait and check all child processes */ int status; pid_t cpid; for(int i=0; i<proc_num; i++) { cpid = wait(&status); if(status!=EXIT_SUCCESS) { puts("Some child process failed"); exit(EXIT_FAILURE); } } /* printf result and total */ int sum = 0; for(int i=0; i<proc_num; i++) { printf("process%d: %d ", i+1, *(rptr+i)); sum += *(rptr+i); } printf("total: %d\n", sum); // close mmap munmap(mptr, len); munmap(rptr, FILE_SIZE); close(fd); close(rfd); return 0; }