用C实现串匹配的几种方法,kmp和BM
本来不想自己写的,但网上的大都是c++实现的,有些自称是C实现的 复制到编译器上根本运行不了。
KMP还是很经典的算法,我就不加注释了,直接返回第几个数匹配..
#include <stdio.h>
#include <stdlib.h>
void getNext(char t[]);
int next[80];
int main()
{
char s[80],t[80];
int result=0;
printf("输入字符串:");
gets(s);
printf("输入字符串:");
gets(t);
getNext(t);
int i=0,j=0;
while(i<strlen(s)&&j<strlen(t))
{
if(s[i]==t[j]) {i++;j++;}
else
{
j=next[j];
if(j==-1) {i++;j++;}
}
}
if(j>=strlen(t)) result=i-j+1;
else result=0;
printf("%d\n",result);
}
getNext(char t[])
{
next[0]=-1;
int j=0,k=-1;
while(j<strlen(t))
{
if(k==-1||t[j]==t[k])
{
j++;
k++;
next[j]=k;
}
else k=next[k];
}
}
}
BM算法,网上实现的不多,有几个版本的都是很复杂,指针来指针去,其实没那么烦
#include <stdio.h>
#include <stdlib.h>
int BM(char s[],char t[],int n,int m);
int dist(char s,char t[]);
int main()
{
char s[80],t[80];
printf("输入字符串:");
gets(s);
printf("输入字符串:");
gets(t);
相关文档:
1.大尾(big_endian)小尾(little_endian)的问题
基于Web的测试软件是由C++数据采集服务器程序和客户端Java显示程序两部分构成,前者用C++,后者Java语言,存在数据移植问题。因为
在计算机系统中,当包含数字的二进制文件从一个结构移到另一结构时,就出现大尾小尾问题。不同CPU在多字节数(如四字节int)存储时有两种方法 ......
掌握文本文件读写的方法
了解二进制文件的读写方法
C++文件流:
fstream // 文件流
ifstream // 输入文件流
ofstream // 输出文件流
//创建一个文本文件并写入信息
//同向屏幕上输出信息一样将信息输出至文件
#include<iomanip.h>
#include<fstream.h>
void main()
{
ofstream f1("d:\\ ......
http://topic.csdn.net/u/20100413/09/ffc0fcd5-c3c1-4a15-b5a2-f1bf65f50caa.html?21874
C/C++ code
typedef struct _VSCSIDISK_COMMON_DEVICE_EXTENSION
{
BOOLEAN IsPdo;
} VSCSIDISK_COMMON_DEVICE_EXTENSION, *PVSCSIDISK_COMMON_DEVICE_EXTENSION;
typedef struct _VSCSIDISK_PDO_DEVI ......
http://msdn.microsoft.com/en-us/library/2e70t5y1(VS.80).aspx
#pragma
pack( n )
n : Valid values are 1, 2, 4, 8, and 16.the
alignment of a member will be on a boundary that is either a multiple of
n
or
a multiple of the size of the member
,
whichever is smaller.
......
#include
void printptr(int *i){
printf("&i of printfptr = %#X\n",&i);
printf("i of printfptr = %#X\n",i);
printf("*i of printfptr = %d\n",*i);
printf("i+1 of printfptr = %#X\n",i+1);
printf("*(i + 1) of printfptr = %d\n",*(i ......