顺序串的模式匹配 朴素算法
2013-08-22 10:01
246 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
理解:用record来记录位移 对每一个可能的位移 比较匹配串和主串子串是否相等。
int Index(char *f,char*c)//father child
{
int fcnt,ccnt,record=0;
for (fcnt=record,ccnt=0;f[fcnt]&&c[ccnt];)//注意此处for循环的出口可以更近 可以更高效。f根本不用比到末尾 只要剩下的字符串的长度不够c了 就可以结束了
{
if (f[fcnt]==c[ccnt])
{
fcnt++;
ccnt++;
}
else
{
record++;
fcnt=record;
ccnt=0;
}
}
if (!c[ccnt]) return record+1;
else return 0;
}
int main()
{
int n,i;
char a[20];
char b[20];
while (1)
{
printf("Input string 1:\n");
gets(a);
printf("Input string 2:\n");
gets(b);
n=Index(b,a);
if(n)
{
for (i=0;i<n-1;i++)printf(" ");
puts(a);
}
else
printf("unmatched\n");
printf("\n");
}
return 0;
}
改进版
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Index(char *f,char*c)//father child
{
int fcnt,ccnt,record=0; //record记录模式串在主串中的位移 0=<record&&record+length(c)<=length(f)
int lenf,lenc;
lenf=strlen(f);
lenc=strlen(c);
for (fcnt=record,ccnt=0;record<=lenf-lenc&&c[ccnt];)
{
if (f[fcnt]==c[ccnt])
{
fcnt++;
ccnt++;
}
else
{
record++;
fcnt=record;
ccnt=0;
}
}
if (!c[ccnt]) return record+1;
else return 0;
}
int main()
{
int n,i;
char a[20];
char b[20];
while (1)
{
printf("Input string 1:\n");
gets(a);
printf("Input string 2:\n");
gets(b);
n=Index(b,a);
if(n)
{
for (i=0;i<n-1;i++)printf(" ");
puts(a);
}
else
printf("unmatched\n");
printf("\n");
}
return 0;
}
#include <stdlib.h>
#include <string.h>
理解:用record来记录位移 对每一个可能的位移 比较匹配串和主串子串是否相等。
int Index(char *f,char*c)//father child
{
int fcnt,ccnt,record=0;
for (fcnt=record,ccnt=0;f[fcnt]&&c[ccnt];)//注意此处for循环的出口可以更近 可以更高效。f根本不用比到末尾 只要剩下的字符串的长度不够c了 就可以结束了
{
if (f[fcnt]==c[ccnt])
{
fcnt++;
ccnt++;
}
else
{
record++;
fcnt=record;
ccnt=0;
}
}
if (!c[ccnt]) return record+1;
else return 0;
}
int main()
{
int n,i;
char a[20];
char b[20];
while (1)
{
printf("Input string 1:\n");
gets(a);
printf("Input string 2:\n");
gets(b);
n=Index(b,a);
if(n)
{
for (i=0;i<n-1;i++)printf(" ");
puts(a);
}
else
printf("unmatched\n");
printf("\n");
}
return 0;
}
改进版
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Index(char *f,char*c)//father child
{
int fcnt,ccnt,record=0; //record记录模式串在主串中的位移 0=<record&&record+length(c)<=length(f)
int lenf,lenc;
lenf=strlen(f);
lenc=strlen(c);
for (fcnt=record,ccnt=0;record<=lenf-lenc&&c[ccnt];)
{
if (f[fcnt]==c[ccnt])
{
fcnt++;
ccnt++;
}
else
{
record++;
fcnt=record;
ccnt=0;
}
}
if (!c[ccnt]) return record+1;
else return 0;
}
int main()
{
int n,i;
char a[20];
char b[20];
while (1)
{
printf("Input string 1:\n");
gets(a);
printf("Input string 2:\n");
gets(b);
n=Index(b,a);
if(n)
{
for (i=0;i<n-1;i++)printf(" ");
puts(a);
}
else
printf("unmatched\n");
printf("\n");
}
return 0;
}
相关文章推荐
- 朴素模式的匹配算法-顺序
- 字符串模式匹配算法之一:朴素模式匹配算法
- 字符串的匹配模式:朴素的模式匹配算法(BF算法)
- 数据结构——串的朴素模式和KMP匹配算法
- C/C++——朴素的模式匹配算法和KMP模式匹配算法
- 模式匹配算法--朴素算法
- (串的模式匹配4.6.1)POJ 3080 Blue Jeans(使用朴素的模式匹配算法求最长的公共子串)
- 朴素的和KMP模式匹配算法
- (11)串的模式匹配:朴素的模式匹配算法,KMP算法
- 2018_1_28_Blue Jeans_模式匹配_朴素_brute_force算法
- 朴素模式匹配算法java实现
- 串的模式匹配算法(一)—朴素的模式匹配算法
- 朴素的模式匹配和改进的模式匹配(KMP)算法说明
- 字符串-朴素的模式匹配算法
- KMP算法与朴素模式匹配算法(C语言)
- 第八周 数据结构之自建算法库——顺序串【项目5 - 计数的模式匹配】
- 数据结构——串的朴素模式和KMP匹配算法
- 朴素的字符串模式匹配算法
- 模式匹配朴素算法习题 算法导论P559
- 数据结构——串的朴素模式和KMP匹配算法