朴素模式匹配(BF)
来源:互联网 发布:python抓取股票数据 编辑:程序博客网 时间:2024/05/01 13:09
public class BF_Match {
public static int match1(String txt,String pat,int offset[]){
int i,j,c=0;
char[] txt2=txt.toCharArray();
char[] pat2=pat.toCharArray();
int m = txt2.length;
int n = pat2.length;
for(i=0;i<=m-n;i++){
for(j=0;j<n;j++){
if(txt2[i+j]!=pat2[j])
break;
if(j==n-1)
offset[c++]=i;
}
}
return c;
}
public static int match2(String txt,String pat,int offset[]){
char[] txt2=txt.toCharArray();
char[] pat2=pat.toCharArray();
int m = txt2.length;
int n = pat2.length;
int i=0;
int j=0;
int c=0;
while(i<m&&j<n){
if(txt2[i]==pat2[j]){
i++;j++;
}else{
i=i-j+1;//
j=0;
}
if(j>=n){//实际j==n
offset[c++]=i-j;
i=i-j+1;
j=0;
}
}
return c;
}
public static void main(String[] args) {
String str1="abcefgefghjefgjjk";
String str2="efg";
int offset[] = new int[11];
int c = match2(str1,str2,offset);
System.out.println(c+"--"+offset[0]+"--"+offset[1]+"--"+offset[2]);
}
}
public static int match1(String txt,String pat,int offset[]){
int i,j,c=0;
char[] txt2=txt.toCharArray();
char[] pat2=pat.toCharArray();
int m = txt2.length;
int n = pat2.length;
for(i=0;i<=m-n;i++){
for(j=0;j<n;j++){
if(txt2[i+j]!=pat2[j])
break;
if(j==n-1)
offset[c++]=i;
}
}
return c;
}
public static int match2(String txt,String pat,int offset[]){
char[] txt2=txt.toCharArray();
char[] pat2=pat.toCharArray();
int m = txt2.length;
int n = pat2.length;
int i=0;
int j=0;
int c=0;
while(i<m&&j<n){
if(txt2[i]==pat2[j]){
i++;j++;
}else{
i=i-j+1;//
j=0;
}
if(j>=n){//实际j==n
offset[c++]=i-j;
i=i-j+1;
j=0;
}
}
return c;
}
public static void main(String[] args) {
String str1="abcefgefghjefgjjk";
String str2="efg";
int offset[] = new int[11];
int c = match2(str1,str2,offset);
System.out.println(c+"--"+offset[0]+"--"+offset[1]+"--"+offset[2]);
}
}
- 朴素模式匹配(BF)
- 1.BF 朴素的模式匹配算法
- 字符串的匹配模式:朴素的模式匹配算法(BF算法)
- 模式匹配-BF算法
- 模式匹配算法(BF)
- 串的操作(一) BruteForce (BF)算法 朴素的模式匹配
- 4.2模式匹配-BF算法
- 【字符串】模式匹配:BF / KMP
- BF模式匹配算法改良
- BF法-字符模式匹配
- 朴素模式匹配
- 朴素模式串匹配
- 字符串模式匹配算法--BF & KMP算法
- 字符串模式匹配之一-------BF & KMP
- 字符串模式匹配之BF算法
- 数据结构12:模式匹配BF算法
- 串的模式匹配--BF算法
- KMP与BF模式匹配算法
- 堆栈的区别及增长方向
- jsp页面的日期显示格式
- uc脚本--字符串
- POJ 1095
- PHP发送邮件类库PHPMailer的简单使用
- 朴素模式匹配(BF)
- iframe自适应高度或自增
- 单侧开门动画
- 开启csdn征程-记录一个软件攻城师的点点滴滴
- C++设计新思维中的判断A是否继承B~
- 编程如何判断一个对象在视图中可见?
- My first C++程序报告
- 开门动画
- 动态添加删除控件