第八周项目五计数的模式匹配
2015-10-26 16:26
513 查看
/* * Copyright (c) 2015, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称: SqString.cpp,SqString.h,main.cpp * 作者:巩凯强 * 完成日期:2015年10月26日 * 版本号:codeblocks * * 问题描述: 采用顺序结构存储串,编写一个算法计算指定子串在一个字符串中出现的次数,如果该子串不出现则为0。 * 输入描述:无 * 程序输出:2 */ #ifndef SQSTRING_H_INCLUDED #define SQSTRING_H_INCLUDED #include <stdio.h> #include <malloc.h> #define MaxSize 100 typedef struct { char data[MaxSize]; //定义可容纳MaxSize个字符的空间 int length; //标记当前实际串长 }SqString; void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串s int StrLength(SqString s); //求串长 void DispStr(SqString s); //输出串 int str_count(SqString s,SqString t); #endif // SQSTRING_H_INCLUDED
#include "sqstring.h" void StrAssign(SqString &s,char cstr[]) { int i; for(i=0;cstr[i]!='\0';i++) { s.data[i]=cstr[i]; } s.length=i; } int StrLength(SqString s) { return s.length; } void DispStr(SqString s) { int i; if(s.length>0) { for(i=0;i<s.length;i++) { printf("%c",s.data[i]); } printf("\n"); } } int str_count(SqString s,SqString t) { int i=0,j=0,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; }
#include "sqstring.h" int main() { SqString s,t; StrAssign(s,"accaccacacabcacbab"); StrAssign(t,"accac"); printf("s:"); DispStr(s); printf("t:"); DispStr(t); printf("%d\n",str_count(s,t)); return 0; }
运行结果:
知识点总结:
本题目是对kmp算法的进一步改进,使得它能够在匹配到之后不直接退出而是继续查找。
学习心得:
加深了对kmp算法的理解。
相关文章推荐
- java list 与map有什么区别??
- sicily1002. 放鸡蛋
- Linux和windows系统直接的文件传输
- 开眼界:网赚就是通过上传下载赚钱
- MySQL定义和变量赋值
- android系统体系结构
- 安卓开发学习之007 LinearLayout实战计算器界面实现
- sublime text3 安装--注册--运行python解释器
- 第六周 项目5-后缀表达式
- 对与locale和resourceBundle的理解
- SQL SERVER错误:已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)
- opencv2-第五章-图像处理之阈值化
- php empty,isset,is_null比较
- SQL SERVER错误:已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)
- rpm的安装与卸载
- 专题 - Web应用->调试Web应用
- C++primer学习:重载运算(2)
- 项目 - 猴子选大王(数组版)
- 第7周项目6--停车场模拟
- 固定栏