请教关于kmp算法,解决立即给分!

redez 2004-11-21 11:06:48
要求:
1)文章存于一个文本文件中。待统计的词汇集合要一次输入完毕,即统计工作必须在程序的一次运行之后就全部完成。程序的输出结果是每个词的出现次数和出现位置所在的行号,格式自行设计。
2)要求采用基于KMP的算法实现。
3)整个统计过程只对文章文字扫描一遍以提高效率。
我编的程序为
#include<stdio.h>
#include<string.h>
void get_next(char *ch,int *next)
{
int i=1,j=0;
while(i<strlen(ch))
{
if(j==0||ch[i]==ch[j]) { i++; j++; next[i]=j; }

else j=next[j];
}
}
int Index_kmp(char *str,char *ch,int *next)
{
int i=0,j=0;
while(j<strlen(ch)&&i<strlen(str))
{
if(str[i]==ch[j]) { j++; i++; }

else
{
if(j==0) i++;
else j=next[j];
}

}
if(j>=strlen(ch)) return (i-j+1);

else return 0;
}
void main()
{
FILE *fp;
char str[100],ch[100],stren[100];
int i=0,m=0,j=0,k=0,next[100];
next[0]=100; next[1]=0;
if((fp=fopen("A.txt","r"))==NULL)
{
printf("File cann't be openen!");
}
while(!feof(fp))
str[i++]=fgetc(fp);
fclose(fp);
printf("\n File A contents are :\n");
printf("%s",str);
printf("\nPlease input the string:\n");
gets(ch);
m=strlen(ch);
get_next(ch,next);
if((i=Index_kmp(str,ch,next))==0)
printf("Nothing about the text's matching\n");
else
{
printf("\nThe position of the matching word is the");
printf(" %dth\n",i);
printf(" %s",&str[i+m-1]);
getchar();
}
}

现在只能查找第一次匹配的位置不能找到所有的匹配的词的次数和出现位置,请问怎么改才能实现
上面的功能?
...全文
182 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
avalonBBS 2004-11-22
  • 打赏
  • 举报
回复
不客气:)
redez 2004-11-22
  • 打赏
  • 举报
回复
ThankYou:)
avalonBBS 2004-11-21
  • 打赏
  • 举报
回复
KMP:
KMP算法查找串S中含串P的个数count
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

inline void NEXT(const string& T,vector<int>& next)
{
//按模式串生成vector,next(T.size())
next[0]=-1;
for(int i=1;i<T.size();i++ ){
int j=next[i-1];
while(T[i]!=T[j+1]&& j>=0 )
j=next[j] ; //递推计算
if(T[i]==T[j+1])next[i]=j+1;
else next[i]=0; //
}
}
inline string::size_type COUNT_KMP(const string& S,
const string& T)
{
//利用模式串T的next函数求T在主串S中的个数count的KMP算法
//其中T非空,
vector<int> next(T.size());
NEXT(T,next);
string::size_type index,count=0;
for(index=0;index<S.size();++index){
int pos=0;
string::size_type iter=index;
while(pos<T.size() && iter<S.size()){
if(S[iter]==T[pos]){
++iter;++pos;
}
else{
if(pos==0)++iter;
else pos=next[pos-1]+1;
}
}//while end
if(pos==T.size()&&(iter-index)==T.size())++count;
} //for end
return count;
}
int main(int argc, char *argv[])
{
string S="abaabcacabaabcacabaabcacabaabcacabaabcac";
string T="ab";
string::size_type count=COUNT_KMP(S,T);
cout<<count<<endl;

system("PAUSE");
return 0;
}

avalonBBS 2004-11-21
  • 打赏
  • 举报
回复
char S[]="abaabcacabaabcacabaabcacabaabcacabaabcac";
char T[]="ab";

输出结果为10,表示S中包含有10个T ,,楼主给分吧~

就等着你的分啦~~马上要升裤头头啦 :)))
avalonBBS 2004-11-21
  • 打赏
  • 举报
回复
呵呵,纯C版的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void NEXT(char * T,int next[])
{
/*按模式串生成next*/
next[0]=-1;
for(unsigned int i=1;i<strlen(T);++i ){
int j=next[i-1];
while(T[i]!=T[j+1]&& j>=0 )
j=next[j] ;
if(T[i]==T[j+1])next[i]=j+1;
else next[i]=0;
}
}
size_t COUNT_KMP(char * S,char * T)
{
/*利用模式串T的next函数求T在主串S中的个数count的KMP算法
其中T非空 */
size_t index,count=0;
int * next= (int *)malloc(sizeof(int)*strlen(T));
NEXT(T,next);
for(index=0;index<strlen(S);++index){
unsigned int pos=0;
size_t iter=index;
while(pos<strlen(T) && iter<strlen(S)){
if(S[iter]==T[pos]){
++iter;++pos;
}
else{
if(pos==0)++iter;
else pos=next[pos-1]+1;
}
}/*while end*/
if(pos==strlen(T)&&(iter-index)==strlen(T))++count;
} /*for end*/
return count;
}
int main(void)
{
size_t count;
char S[]="abaabcacabaabcacabaabcacabaabcacabaabcac";
char T[]="ab";
count=COUNT_KMP(S,T);
printf("%u",count);

system("PAUSE");
return 0;
}
redez 2004-11-21
  • 打赏
  • 举报
回复
要是c语言的应该怎么做呢

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧