分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Brute-Force算法的思想
1.BF(Brute-Force)算法
Brute-Force算法的基本思想是:
1) 从目标串s 的第一个字符起和模式串t的第一个字符进行比较,若相等,则继续逐个比较后续字符,否则从串s 的第二个字符起再重新和串t进行比较。
2) 依此类推,直至串t 中的每个字符依次和串s的一个连续的字符序列相等,则称模式匹配成功,此时串t的第一个字符在串s 中的位置就是t 在s中的位置,否则模式匹配不成功。
Brute-Force算法的实现
c语言实现:
// Test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include "stdlib.h"#include <iostream>using namespace std;//宏定义 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define MAXSTRLEN 100typedef char SString[MAXSTRLEN + 1];/************************************************************************//* 返回子串T在主串S中第pos位置之后的位置,若不存在,返回0*//************************************************************************/int BFindex(SString S, SString T, int pos){ if (pos <1 || pos > S[0] ) exit(ERROR); int i = pos, j =1; while (i<= S[0] && j <= T[0]) { if (S[i] == T[j]) { ++i; ++j; } else { i = i- j+ 2; j = 1; } } if(j > T[0]) return i - T[0]; return ERROR;}void main(){ SString S = {
13,'a'