我要小红花:PP

0 views
Skip to first unread message

int 0x080

unread,
Oct 17, 2006, 5:57:45 AM10/17/06
to c语言基地
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>

static int *
get_next( const char *const str )
{
int *next,len=strlen(str),i,j;
assert(NULL!=(next=(int *)malloc(sizeof(int)*(len))));
for(next[0]=-1,i=1,j=-1; i<len; ++i ){
for(j=i-1;j!=-1&&str[j]!=str[next[j]];j=next[j])
;
next[i]=(j!=-1&&str[j]==str[next[j]])?next[j]+1:0;
}
return next;
}

static int *
get_nextval( const char *const str )
{
int i,j,len=strlen(str),*next=get_next( str );
for(i=1; i<len; ++i){
if((j=next[i])!=0&&str[i]==str[j])
next[i]=next[j];
}
return next;
}

#define MAX 256

int
main(int argc, char **argv)
{
FILE * input=NULL;
char buf[MAX],patt[MAX];

if(argc != 2){
fprintf(stderr,"Usage:./a.out <Input_file_name>");
exit(1);
}
if( NULL == (input=(fopen(argv[1],"r"))) ){
fprintf(stderr,"%s==>%s",argv[1],strerror(errno));
exit(1);
}
fprintf(stdout,"patt:");
scanf("%s",patt);

int count=0;/*这个是来统计的变量*/
int * next=get_nextval(patt);
while(NULL != fgets(buf,MAX,input)){
int i=0,j=0;
while(i<=strlen(buf)){
if(j==strlen(patt))j=0,++count;
j=(-1==j)?(++i,0):buf[i]==patt[j]?(++i,j+1):next[j];
}
}
fprintf(stdout,"%d",count);
fclose(input);
exit(0);
}

汪军

unread,
Oct 18, 2006, 9:33:04 PM10/18/06
to hy...@googlegroups.com
注释好少啊...

来来去去

unread,
Oct 18, 2006, 10:15:06 PM10/18/06
to c语言基地
是KMP

int 0x080

unread,
Oct 19, 2006, 12:32:02 AM10/19/06
to c语言基地
就是KMP匹配算法,nextval的求值书上讲的更详细的:)

汪军 写道:

> 注释好少啊...

汪军

unread,
Oct 19, 2006, 9:09:07 PM10/19/06
to hy...@googlegroups.com
注意学习编程规范啊,虽然我学得也不好,但行内人必须注意的规矩

在06-10-19,int 0x080 <int....@gmail.com> 写道:
就是KMP匹配算法,nextval的求值书上讲的更详细的:)

汪军 写道:

> 注释好少啊...


汪军的签名

colitas

unread,
Oct 20, 2006, 6:31:31 AM10/20/06
to c语言基地
例如具体的什么地方?

rain.man

unread,
Oct 20, 2006, 9:26:24 AM10/20/06
to hy...@googlegroups.com
我没读过这方面的书,能介绍本吗.

在06-10-20,汪军 <hyd...@gmail.com> 写道:



--
  ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
        ╲█◤
      ◢█◥◣ Good Good Life , Day Day Study !  ◣◢
        ◤◥                  -- rainman      ◥◣█◤
  _____________________________________________◢█╲ _____

汪军

unread,
Oct 24, 2006, 11:31:21 PM10/24/06
to c语言基地
http://groups.google.com/group/hydil/browse_thread/thread/ae6d6217e158fbff/18ab7743126edaee?lnk=gst&q=%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83&rnum=1#18ab7743126edaee
网址在上面,你也可以用google搜索c++编程规范,希望愿意加入程序员行列的朋友都读一读,很有用处

colitas

unread,
Oct 27, 2006, 12:32:19 AM10/27/06
to c语言基地
嗯,好的

Yiqiang Luo

unread,
Nov 6, 2006, 6:30:43 AM11/6/06
to hy...@googlegroups.com
为什么不整个Word文档附件呢?
我下载下来默认Txt格式,没有换行,恼火死了……


2006/10/25, 汪军 <hyd...@gmail.com>:
http://groups.google.com/group/hydil/browse_thread/thread/ae6d6217e158fbff/18ab7743126edaee?lnk=gst&q=%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83&rnum=1#18ab7743126edaee
网址在上面,你也可以用google搜索c++编程规范,希望愿意加入程序员行列的朋友都读一读,很有用处

汪军

unread,
Nov 6, 2006, 7:48:31 PM11/6/06
to hy...@googlegroups.com
有附近啦,呵呵,大家有好东西分享啊.
如果要想作为程序员,这个必须要看的.


--
汪军的签名
高质量C++/C编程指南.doc

xue hang

unread,
Nov 6, 2006, 11:34:12 PM11/6/06
to hy...@googlegroups.com
非常感谢阿,小红花?怎么给??:)赫赫

在06-11-7,汪军 <hyd...@gmail.com> 写道:
有附近啦,呵呵,大家有好东西分享啊.
如果要想作为程序员,这个必须要看的.


--
汪军的签名




Yiqiang Luo

unread,
Nov 7, 2006, 9:08:21 AM11/7/06
to hy...@googlegroups.com
不错,响应很快,表扬一下!呵呵
多谢了!!!
 
Reply all
Reply to author
Forward
0 new messages