题目3:文本文件单词的检索与计数(实现代码)
生活随笔
收集整理的這篇文章主要介紹了
题目3:文本文件单词的检索与计数(实现代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
聲明
BF的話主要是為了體現出KMP的高效之處,當然比BF更高效的還有BM,這里的話就不一一過多解釋
BF算法
#include<iostream> using namespace std; //返回子串t在主串t中第pos個字符之后的位置;若不存在,函數返回0 //t非空,1≤pos≤s.size() int Index(string s, string t, int pos=0) //默認為0,默認從第一個元素開始查找 {int i = pos; //主串t當前下表int j = 0; //子串t當前下表while (i<s.size() && j<t.size()) //i小于s長度,j小于t長度,循環繼續{if (s[i] == t[j]){i++;j++;}else //指針退回,重新開始匹配{i = i - j + 1; //i退回到上一次匹配首位的下一位j = 0; //j退回到子串t的首位}}if (j >= t.size())return i - t.size() + 1; //i - t.size()表示從s[4]開始重復,+1表示該元素為第5個元素elsereturn 0; }int main() {string s = "goodgoogle";string t = "google";cout << Index(s, t)<<endl;return 0; }KMP算法
原版
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<Windows.h> #define BUF_SIZE 256 typedef struct seqstring {char string[100];int length; }seqstring;void getnext(seqstring p, int next[]) {int i, j;i = 0;//指向字符串每個字符的下標j = -1;next[i] = j;//next[0]放上-1 while (i < p.length) {//沒有到達結尾的話 if (j == -1 || p.string[i] == p.string[j]) {//如果是第一個字符或遇到相同的字符next[++i] = ++j;}else {j = next[j];}}for (i = 0;i < p.length;i++) {//輸出next[]值 printf("%d ", next[i]);} }int kmp(seqstring t, seqstring p, int next[]) {int i, j;i = j = 0;while (i < t.length && j < p.length) {if (j == -1 || t.string[i] == p.string[j]) {i++;j++;}else {j = next[j];}}if (j == p.length) return i - p.length;else return -1; } int main() {seqstring t, p;int next[50];DWORD nIn;char buffer[BUF_SIZE] = "";HANDLE handle = CreateFile("test.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);if (handle == INVALID_HANDLE_VALUE) {printf("%d", GetLastError());return -1;}ReadFile(handle, buffer, BUF_SIZE, &nIn, NULL) ;strcpy(t.string, buffer);printf("%s\n", t.string);t.length = strlen(t.string);printf("please input string p:");scanf("%s", p.string);printf("%s\n", p.string);p.length = strlen(p.string);printf("next:");getnext(p, next);printf("\n%d\n", kmp(t, p, next)); }
改進版(符合題意版)
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<Windows.h> #include <iostream> using namespace std; #define BUF_SIZE 256 int counts = 0; int direction[200] = {0}; typedef struct seqstring {char string[100];int length; }seqstring;void getnext(seqstring p, int next[]) {int i, j;i = 0;//指向字符串每個字符的下標j = -1;next[i] = j;//next[0]放上-1 while (i < p.length) {//沒有到達結尾的話 if (j == -1 || p.string[i] == p.string[j]) {//如果是第一個字符或遇到相同的字符next[++i] = ++j;}else {j = next[j];}}for (i = 0; i < p.length; i++) {//輸出next[]值 //printf("%d ", next[i]);} }int kmp(seqstring t, seqstring p, int next[]) {int i, j;i = j = 0;while (true) {if (j == -1 || t.string[i] == p.string[j]) {i++; j++;}else {j = next[j];}if (!(i < t.length && j < p.length)) {if (j == p.length) {direction[counts] = i - p.length;counts++;j = 0;}if (i == t.length) {return 0;}}}if (j == p.length) { return i - p.length;}else{return -1;}} int main() {seqstring t, p;int next[50];DWORD nIn;char buffer[BUF_SIZE] = "";HANDLE handle = CreateFile("test.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);if (handle == INVALID_HANDLE_VALUE) {printf("%d", GetLastError());return -1;}ReadFile(handle, buffer, BUF_SIZE, &nIn, NULL);strcpy(t.string, buffer);printf("%s\n", t.string);t.length = strlen(t.string);printf("please input string p:");scanf("%s", p.string);printf("%s\n", p.string);p.length = strlen(p.string);//printf("next:");getnext(p, next);kmp(t, p, next);printf("\n%d\n", counts);printf("單詞出現的位置如下:\n");for (int i = 0; i < counts; i++) {cout << direction[i]<<" ";} }查找結果:
總結
以上是生活随笔為你收集整理的题目3:文本文件单词的检索与计数(实现代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML+CSS实战作业
- 下一篇: 题目2:隐式图的搜索问题(A*算法解决八