//几个算法写得老阔痛
#ifndef PCH_H
#define PCH_H
#include<cstdlib>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<math.h>
#include<string>//?
#include<time.h>//時間計算
constexpr auto MAXSIZE = 255;
constexpr auto OK = 1;
constexpr auto ERROR = 0;
constexpr auto CHUNKSIZE = 80;//可由用户定义的块大小
constexpr auto TSIZE = 5;
typedef int Status;
//--------串的定长顺序存储结构----------
typedef struct {
char ch[MAXSIZE + 1];//存储串的一维数组,最后一位给'\0'
int length;//串的当前长度
}SString;//静态定义,编译时就确定了串的空间大小
//--------heap 堆式顺序存储结构---------
typedef struct {
char *ch;//按照串长分配存储区,否则ch为NULL
int length;//串的当前长度
}HString;
//----------串的块链存储结构---------
typedef struct Chunk{
//需要大量移动字符
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk;
typedef struct
{
//串操作实现与线性表在链式存储结构中操作类似
Chunk *head,*tail;//串头串尾指针
int length;//串的当前长度
}LString;
//---Brute-Force 算法---
int Index_BF(SString S/*主串*/, SString T/*模式串*/, int pos/*查找起始位置*/);
Status StrPrint(SString T);
Status StrInit(SString &T);
Status StrAssign(SString &T,char *ch);//ch为字符串常量,生成值为ch的串
Status StrCopy(SString &T, SString S);//由串S复制得串T
Status StrEmpty(SString S);//若S为空串返回true
Status StrCompare(SString S, SString T);
/*S>T返回>0,S=T返回=0,S<T返回<0*/
Status StrLength(SString S);
Status ClearString(SString &S);//将S清为空串
Status Concat(SString &T,SString S1,SString S2);//T返回S1和S2联结而成的新串
Status SubString(SString &Sub, SString S,int pos, int len);
//用串sub返回串S第pos位置长度len的串
void Replace(SString &S, SString T, SString V);//用V替换所有与T相同不重叠子串
Status StrInsert(SString &S, int pos, SString T);//在串S的第pos位置之前插入串T
Status StrDelete(SString &S, int pos, int len);//从串S中删除第pos位置起长度为len的子串
Status DestroyString(SString &S);//串S存在,串S销毁
#endif
-------------------------------------------------
// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的
#include "pch.h"
// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
int Index_BF(SString S, SString T, int pos)
{
//BF算法
//返回模式T在主串S中第pos个字符开始 第一次出现的位置
//不存在则返回0
int i = pos-1;//初始化i,主串S中第pos个字符
int j
顺序存储串的基本操作
最新推荐文章于 2022-07-05 11:47:13 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)