栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

串基本操作

串链

KMP算法


串基本操作

头文件

#pragma once
#include
#include
#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] = '';
}
//串比较 比较两个串的大小 若s1>s2 则返回正数 否则返回负数和0
int StrCmp(char*s1, char*s2)
{
	int i = 0;
	while (s1[i] != ''&&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] = '';
	return 1;
}
int main()
{
	return 0;
}

串链
#include "4.2串链.h"
#include 
#include 
#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 
#include 
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];
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号