2021-10-11 数据结构与算法 4 串

目录

串基本操作

串链

KMP算法


串基本操作

头文件

#pragma once
#include<stdlib.h>
#include<stdio.h>
#include"4.1串.h"
#define maxsize 1024
typedef struct
{
	char ch[maxsize];
	int length;
}SeqString;
SeqString *s;
//求串长
int StrLength(char*s);
//串赋值(串赋值)
void StrCopy(char*s1, char*s2);
//串比较 比较两个串的大小 若s1>s2 则返回正数 否则返回负数和0
int StrCmp(char*s1, char*s2);
//串连接
int StrCat(char*s1, char*s2);

实现

#define maxsize 1024
//求串长
int StrLength(char*s)
{
	int i = 0;
	while (s[i] != '0')
	{
		i++;
	}
	return i;
}

//串赋值(串赋值)
void StrCopy(char*s1, char*s2)
{
	int i = 0;
	while (s2[i] != '0')
	{
		s1[i] = s2[i];
		i++;
	}
	s1[i] = '\0';
}
//串比较 比较两个串的大小 若s1>s2 则返回正数 否则返回负数和0
int StrCmp(char*s1, char*s2)
{
	int i = 0;
	while (s1[i] != '\0'&&s1[i] == s2[i])
	{
		i++;
	}
	return s1[i] - s2[i];
}
//串连接
int StrCat(char*s1, char*s2)
{
	int len1 = StrLength(s1);
	int len2 = StrLength(s2);
	if (len1 + len2 > maxsize)
	{
		return 0;
	}
	int i,j ;
	i = j = 0;
	while (s1[i] != 0)
	{
		i++;
	}
	while (s2[j] != 0)
	{
		s1[i] = s2[j];
		i++;
		j++;
	}
	s1[i] = '\0';
	return 1;
}
int main()
{
	return 0;
}

串链

#include "4.2串链.h"
#include <stdio.h>
#include <stdlib.h>
#define CHUNKSIZE 4
typedef struct node
{
	char ch[CHUNKSIZE];
	struct node *next;
}Chunk;
typedef struct
{
	Chunk *head, *tail;
	int length;
}LinkString;
LinkString s;


int main()
{
	return 0;
}

KMP算法

#include <iostream>
#include <cstring>
using namespace std;

int Index_KMP(string T, string P, int next[])
{
	int i, j;
	i = begin; j = 0;//设置目标串和模式串的匹配位置
	while (i <= strlen(T) && j <= strlen(p))
	{
		if (j == -1 || T[i] == P[i]) { i++; j++; }
		else j = next[j];//回溯next位置
	}
	if (j > strlen(P)) return i - strlen(P);
	else return 0;
}

void Get_Next(string P, int next[])
{
	int i, j;
	j = 0; k = -1; next[0] = -1;
	while (j < strlen(P))
	{
		if(k == -1 || P[j] == P[k]) { j++; k++; next[j] = k; }
		else k = next[k];
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值