第八周项目5-计数的模式匹配
来源:互联网 发布:官方淘宝网下载安装 编辑:程序博客网 时间:2024/06/16 18:28
问题及代码:
/*copyright (t) 2016,烟台大学计算机学院*All rights reserved.*文件名称:1.cpp*作者:常锐*完成日期:2016年10月21日*版本号:v1.0*问题描述:采用顺序结构存储串,编写一个算法计算指定子串在一个字符串中出现的次数,如果该子串不出现则为0。*输入描述:字符串(设为多组输入)*程序输出:指定子串在一个字符串中出现的次数*/
sqstring.h:
#include <stdio.h>#define MaxSize 100typedef struct //定义顺序串类型{ char data[MaxSize]; //存放字符 int length; //记录串长度} SqString;void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串svoid StrCopy(SqString &s,SqString t); //串t复制给串sbool StrEqual(SqString s,SqString t); //判串相等int StrLength(SqString s); //求串长SqString Concat(SqString s,SqString t); //串连接SqString SubStr(SqString s,int i,int j); //求子串SqString InsStr(SqString s1,int i,SqString s2); //串插入SqString DelStr(SqString s,int i,int j) ; //串删去SqString RepStr(SqString s,int i,int j,SqString t); //串替换void DispStr(SqString s); //输出串
sqstring.cpp:
#include <stdio.h>#include "sqstring.h"int index(SqString s,SqString t){ int i=0,j=0; int count=0; while(i<s.length && j<t.length) { if(s.data[i]==t.data[j]) i++,j++; else { i=i-j+1; j=0; } if(j>=t.length) //完成一次匹配后,次数+1 { count++; i=i-j+1; //主串从下一位置开始匹配,子串从头开始匹配 j=0; } } return count;}int main(){ SqString s1,s2; //s1为主串,s2为模式串 char a[1000],b[1000]; while(gets(a)) { gets(b); StrAssign(s1,a); StrAssign(s2,b); printf("%d\n\n",index(s1,s2)); } return 0;}
main.cpp:
#include <stdio.h>#include "sqstring.h"int index(SqString s,SqString t){ int i=0,j=0; int count=0; while(i<s.length && j<t.length) { if(s.data[i]==t.data[j]) i++,j++; else { i=i-j+1; j=0; } if(j>=t.length) { count++; i=i-j+1; j=0; } } return count;}int main(){ SqString s1,s2; //s1为主串,s2为模式串 char a[1000],b[1000]; while(gets(a)) { gets(b); StrAssign(s1,a); StrAssign(s2,b); printf("%d\n\n",index(s1,s2)); } return 0;}
运行结果:
知识点总结:
串的模式匹配
心得体会:
通过此项目,我加深了对BF和KMP算法的理解与应用,能够打开思维,深入思考。
0 0
- 第八周项目5-计数模式匹配
- 第八周--项目5计数的模式匹配
- 第八周 项目5-计数的模式匹配
- 第八周项目5计数的匹配模式
- 第八周-项目5-计数的模式匹配
- 第八周上机实践项目5 - 计数的模式匹配
- 第八周 项目5-计数的模式匹配
- 第八周项目5——计数的模式匹配
- 第八周项目5-计数的模式匹配
- 第八周项目5-计数的模式匹配
- 第八周项目5--计数的模式匹配
- 第八周 项目5-计数模式的匹配
- 第八周项目5—计数的模式匹配
- 第八周项目5-计数的模式匹配
- 第八周实践项目5--计数的模式匹配
- 第八周项目5 计数的模式匹配
- 第八周 项目5 计数的模式匹配
- 第八周 项目5-计数的模式匹配
- 启动图删除后上下有黑边
- Unity批量创建按钮
- jquery根据图片宽度同比例设置图片高度,图片自适应高度
- 洛谷 2458
- Android Studio解决unspecified on project app resolves to an APK archive which is not supported
- 第八周项目5-计数的模式匹配
- 将一个十六进制的数组转换成一个十六进制数
- eshell操作
- 【已解决】Uncaught SyntaxError: Unexpected token o in JSON at position 1
- RAD Studio 2016-2017 Roadmap Update 及 10.1 Berlin update 2 相关信息
- 给Python初学者的一些技巧
- Linux证书登录和SSH登录
- C++ vector详解
- RecyclerView增加和删除效果