目录
串基本操作
头文件
#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];
}
}