KMP中文字符匹配算法的C++实现
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 170 2015-01-29 10:49 使用说明.txt
文件 320 2015-01-25 15:35 fuzzyMatchookStack.txt
文件 11674 2015-01-29 10:43 fuzzyMatchDebugBuildLog.htm
文件 498688 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe
文件 406 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.embed.manifest
文件 472 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.embed.manifest.res
文件 381 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.intermediate.manifest
文件 843204 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.ilk
文件 816948 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.obj
文件 2739200 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.pdb
文件 65 2015-01-29 10:43 fuzzyMatchDebugmt.dep
文件 91136 2015-01-25 15:33 fuzzyMatchDebugvc60.idb
文件 151552 2015-01-25 15:30 fuzzyMatchDebugvc60.pdb
文件 183296 2015-01-29 10:43 fuzzyMatchDebugvc90.idb
文件 241664 2015-01-29 10:43 fuzzyMatchDebugvc90.pdb
文件 784295 2008-10-24 21:50 fuzzyMatchdictiory.txt
文件 9319 2015-01-29 10:55 fuzzyMatchfuzzySearch.cpp
文件 3461 2015-01-24 14:52 fuzzyMatchfuzzySearch.dsp
文件 528 2015-01-24 17:34 fuzzyMatchfuzzySearch.dsw
文件 2444288 2015-01-29 10:55 fuzzyMatchfuzzySearch.ncb
文件 48640 2015-01-25 00:52 fuzzyMatchfuzzySearch.opt
文件 7536 2015-01-25 15:30 fuzzyMatchfuzzySearch.plg
文件 887 2015-01-29 10:43 fuzzyMatchfuzzySearch.sln
..A..H. 8192 2015-01-29 10:55 fuzzyMatchfuzzySearch.suo
文件 4901 2015-01-29 10:43 fuzzyMatchfuzzySearch.vcproj
文件 1427 2015-01-29 10:55 fuzzyMatchfuzzySearch.vcproj.2012-20141127ES.Administrator.user
文件 22 2015-01-29 10:44 fuzzyMatch
esOfdivid.txt
文件 0 2015-01-29 10:44 fuzzyMatch
esult.txt
文件 14 2015-01-29 10:44 fuzzyMatchsearchFor.txt
文件 16 2015-01-25 11:05 fuzzyMatchsou.txt
............此处省略5个文件信息
//#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int s1 = 0XB0s2 = 0XA1e1 = 0XF8e2 = 0XFF;
const int maxwordlen = 50;
struct Second
{
string key;
Second *next;
Second(string k = ““Second *n = 0):key(k)next(n){}
};
struct Head
{
int size;
string key;
vector W;
Head(string k = ““int s = 0):key(k)size(s){}
};
void get_nextval(const char *T int next[])
{
// 求模式串T的next函数值并存入数组 next。
int j = 0 k = -1;
next[0] = -1;
while ( T[j/*+1*/] != ‘ ‘ )
{
if (k == -1 || T[j] == T[k])
{
++j; ++k;
if (T[j]!=T[k])
next[j] = k;
else
next[j] = next[k];
}// if
else
k = next[k];
}// while
////这里是我加的显示部分
// for(int i=0;i //{
// cout< //}
//cout< }
//下面是KMP模式匹配程序,各位可以用他验证。记得加入上面的函数
int KMP(const char *Textconst char* Pattern) //const 表示函数内部不会改变这个参数的值。
{
if( !Text||!Pattern|| Pattern[0]==‘ ‘ || Text[0]==‘ ‘ )//
return -1;//空指针或空串,返回-1。
int len=0;
const char * c=Pattern;
while(*c++!=‘ ‘)//移动指针比移动下标快。
{
++len;//字符串长度。
}
int *next=new int[len+1];
get_nextval(Patternnext);//求Pattern的next函数值
int index=0i=0j=0;
while(Text[i]!=‘ ‘ && Pattern[j]!=‘ ‘ )
{
if(Text[i]== Pattern[j])
{
++i;// 继续比较后继字符
++j;
}
else
{
index += j-next[j];
if(next[j]!=-1)
j=next[j];// 模式串向右移动
else
{
j=0;
++i;
}
}
}//while
delete []next;
if(Pattern[j]==‘ ‘)
return index;// 匹配成功
else
return -1;
}
//Dictiory类为中文分词,此处未用到,只是因项目需求就做了下,大家若有需要可以参考。
class Dictiory
{
vector H;
ifstream fin;
ifstream fcin;
ofstream fout;
int hash[e1 - s1][e2 - s2];
int BinarySearch(string strint k);
int GetNum();
void LoadDic();
bool IsC(char c);
bool IsEc(char c);
void AddWord(string strint k);
void InsertWord(string strint k);
bool IsWord(string strint kint t);
void SkipNotChinese(string &strstack &stk);
public:
Dictiory(string sfilenamestring dfilename);
void SegmentWord(string s);
void PrintDic()
{
for(int i = 0; i < e1 - s1;i++)
for(int j = 0; j < e2 - s2;j++)
{
if(hash[i][j] >= 0)
{
fout << H[hash[i][j]].key << en
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 170 2015-01-29 10:49 使用说明.txt
文件 320 2015-01-25 15:35 fuzzyMatchookStack.txt
文件 11674 2015-01-29 10:43 fuzzyMatchDebugBuildLog.htm
文件 498688 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe
文件 406 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.em
文件 472 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.em
文件 381 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.exe.intermediate.manifest
文件 843204 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.ilk
文件 816948 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.obj
文件 2739200 2015-01-29 10:43 fuzzyMatchDebugfuzzySearch.pdb
文件 65 2015-01-29 10:43 fuzzyMatchDebugmt.dep
文件 91136 2015-01-25 15:33 fuzzyMatchDebugvc60.idb
文件 151552 2015-01-25 15:30 fuzzyMatchDebugvc60.pdb
文件 183296 2015-01-29 10:43 fuzzyMatchDebugvc90.idb
文件 241664 2015-01-29 10:43 fuzzyMatchDebugvc90.pdb
文件 784295 2008-10-24 21:50 fuzzyMatchdictiory.txt
文件 9319 2015-01-29 10:55 fuzzyMatchfuzzySearch.cpp
文件 3461 2015-01-24 14:52 fuzzyMatchfuzzySearch.dsp
文件 528 2015-01-24 17:34 fuzzyMatchfuzzySearch.dsw
文件 2444288 2015-01-29 10:55 fuzzyMatchfuzzySearch.ncb
文件 48640 2015-01-25 00:52 fuzzyMatchfuzzySearch.opt
文件 7536 2015-01-25 15:30 fuzzyMatchfuzzySearch.plg
文件 887 2015-01-29 10:43 fuzzyMatchfuzzySearch.sln
..A..H. 8192 2015-01-29 10:55 fuzzyMatchfuzzySearch.suo
文件 4901 2015-01-29 10:43 fuzzyMatchfuzzySearch.vcproj
文件 1427 2015-01-29 10:55 fuzzyMatchfuzzySearch.vcproj.2012-20141127ES.Administrator.user
文件 22 2015-01-29 10:44 fuzzyMatch
esOfdivid.txt
文件 0 2015-01-29 10:44 fuzzyMatch
esult.txt
文件 14 2015-01-29 10:44 fuzzyMatchsearchFor.txt
文件 16 2015-01-25 11:05 fuzzyMatchsou.txt
............此处省略5个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)