字符串的相关算法

字符串相关的算法整理 本文根据个人学习过程,积累一些常用知识。供后续参考学习。 字符串的相关的算法,一直是面试和项目中经常用到的。因此,在这里整理记录。字符串可以看成是字符类型的数组,因此可以将其和数组排序、数组查找和顺序调整有关的算法相结合。另外,字符串的比较和子字符串的操作,可以用来解决其他问题,比如一棵树是否和另一棵树的子树相匹配。 字符串操作包含很多基本知识点,如:回文的概念、字符匹配的问题、字典序问题、子 阅读详情

#include<iostream.h>
#include<string.h>
#include<malloc.h>
#define MaxLen 20
typedef struct
{
 char ch[MaxLen];
 int len;
}strtype;


void create(strtype *s,char str[])
{
 strcpy(s->ch,str);
 s->len=strlen(str);

}


int len(strtype *s)
{
 return s->len;
}


strtype *copy(strtype *s,strtype *t)
{
 for(int i=0;i<len(t);i++)
 {
  s->ch[i]=t->ch[i];
 }
 s->ch[len(t)]='\0';
 s->len=t->len;
 return s;
}


strtype *concat(strtype *s,strtype *t)
{
 if(len(s)+len(t)>MaxLen-1)
 {
  cout<<"长度不够"<<endl;
  return s;
 }
 int i,j;
 for(i=len(s),j=0;j<len(t);i++,j++)
 {
  s->ch[i]=t->ch[j];
 }
 s->ch[len(s)+len(t)]='\0';
 s->len=len(s)+len(t);
 return s;
}


strtype *ins(strtype *s,strtype *t,int i)
{
 int j,k;
 if(i<0 || i>len(s)+1)
 {
  cout<<"i不合理"<<endl;
  return s;
 }
 if(i+len(t)-1>MaxLen)
 {
  cout<<"空间不够"<<endl;
  return s;
 }
 if(i==1)
 {
  for(j=len(t),k=0;k<len(s);j++,k++)
   t->ch[j]=s->ch[k];
  t->ch[j]='\0';
  t->len=t->len+s->len;
  return t;
 }
 if(i==len(s))
  return concat(s,t);
 for(j=len(s)-1;j>=i-1;j--)
 {
  s->ch[j+len(t)]=s->ch[j];
 }
 s->ch[len(s)+len(t)]='\0';
 for(k=0,j=i-1;k<len(t);k++,j++)
  s->ch[j]=t->ch[k];
 return s;
}


strtype *del(strtype *s,int pos,int n)
{
 int i,j;
 if(pos+n>len(s))
 {
  cout<<"没那么多元素"<<endl;
  return s;
 }
 if(pos<0 || pos>len(s))
 {
  cout<<"pos不合格"<<endl;
  return s;
 }
 if(pos-1+n==len(s))
 {
  s->ch[pos-1]='\0';
  s->len=s->len-n;
  return s;
 }

 for(j=pos-1+n;j<=len(s);j++)
 {
  s->ch[j-n]=s->ch[j];
 }
 s->len=s->len-n;
 return s;
}

 

void dis(strtype *s)
{
 for(int i=0;i<len(s);i++)
  cout<<s->ch[i]<<" ";
 cout<<endl;
}

【深度学习|地学应用】MODIS AOD产品和HYSPLIT模型的局限性和不确定性? 阅读详情

相关推荐

海康工业相机BayerRG8转BGR8实战:手把手教你用OpenCV实现高效图像转换

本文详细解析了海康工业相机BayerRG8格式到BGR8格式的高效转换实战。针对工业视觉项目中高帧率、大分辨率场景下的性能瓶颈,文章对比了海康SDK原生方案与OpenCV多种转换方法的优劣,并提供了从基础实现到高级优化(如多线程流水线、内存管理、SIMD指令集应用)的完整代码示例与性能调优策略,帮助开发者构建稳定、高效的图像处理系统。

weixin_29237635的博客 524

从串s中删除所有和串t相同的子串的算法

编写算法,从串s中删除所有和串t相同的子串。 StringType是串的一个抽象数据类型,它包含以下6种基本操作: void InitStr(StringType &s);   // 初始化s为空串。 void StrAssign(StringType &t, StringType s);   // 将s的值赋给t。s的实际参数是串变量。 int StrCompare(StringTy

积极奋斗的蜗牛 1万+

Arduino智能小灯DIY:人体感应+光线感应+蓝牙控制全攻略(附完整代码)

本文提供了一份完整的Arduino智能小灯DIY制作指南。项目融合了人体感应、光线感应与蓝牙控制三大功能,通过详细的硬件清单、电路连接图、核心逻辑讲解及附带的完整代码,手把手教你打造一个能自动感知环境与人、并支持手机远程控制的智能照明装置,是入门智能家居和物联网开发的绝佳实践。

java5的博客 1094

数据结构基础学习——字符串匹配KMP算法

数学功底偏弱,最近花了1天时间才理解了用于字符串匹配的KMP算法。 KMP算法是由 Knuth、Pratt 和 Morris 三个人同时发现的,因此取名为KMP,并不是什么功能性短语的缩写。该算法主要希望:“在字符串匹配的过程中尽可能地减少重复比较的次数,从而提高匹配效率”。 假设有源字符串S和模式字符串T,现在需要设计一个函数来查找S串中与T串相同的子串所在的位置i,若子串不存在,则返回0。 typedef struct string { uint Length; char *Ch; } S

Frankenstien_的博客 549

告别反复刷写:用英飞凌XCP Overlay功能实现ECU标定与诊断的‘热更新’

本文详细介绍了英飞凌XCP Overlay功能在ECU标定与诊断中的应用,实现参数的'热更新',避免反复刷写Flash。通过地址重映射技术,将标定变量从Flash重定向到RAM,显著提升工作效率并保护Flash寿命。文章涵盖技术原理、工程实践、功能安全考量及高级应用场景,为汽车电子工程师提供完整解决方案。

weixin_30689307的博客 533

PPO算法详解:基于CartPole-v1的强化学习实现

PPO算法详解:基于CartPole-v1的强化学习实现

m0_54720701的博客 865

【算法】字符串相关

字符串是一种数据结构,大多与别的算法结合在一起出题,例如模拟、高精度算法、双指针、dp、回溯等,因此这个专题的题型本身是特别丰富的。本篇选取了较为典型的字符串题型,除了涵盖一些常用算法,还包括经常用到的容器、接口等。

NeeEk0的博客 1156

【STK】手把手教你利用STK进行进行光电红外仿真02-STK/EOIR模块属性之一 传感器选项设置

EOIR传感器可以从空间几何属性、光谱属性、光学属性、辐射属性四个方面进行设置,并且可以设置信号传播路径中的大气特性,包括大气模型、气溶胶模型、气象学可见性和大气湿度。在定义EOIR传感器时,需要Sensor——Definition属性页中,将Sensor Type设置为EOIR。单击下面的按钮打开传感器的EOIR配置页,可以对传感器的属性进行配置,此外,还可以在场景中视情况添加相关的中心体。路径辐射损耗影响上述特性的计算,计算的结果考虑了传感器、目标位置的影响,但是还没考虑地球临边背景的影响。

weixin_44184852的博客 3201

字符串相关算法

整体思路就是模拟列竖式计算两个数相乘的过程。但是为了书写代码的方便性,我们选择优化的版本,就是在计算两数相乘的时候,先不考虑进位,等到所有结果计算完毕之后,再去考虑进位。

2302_77179144的博客 634

【Claude】Not logged in 错误:未登录状态的判断与 /login 认证路径 bug报错已解决

login 是首选修复:大多数 "Not logged in" 只需重新执行 /login检查系统时间:时间偏差是 Token 验证失败的常见原因避免密钥库被清理:将 Claude Code 添加到系统清理工具的白名单企业环境用 API Key:如果组织禁用 OAuth,使用 API Key 认证区分凭证类型:理解 Not logged in(无凭证)vs Invalid key(有但无效)

博客 7316

线程池详解(通俗易懂超级好)

目标 【理解】线程池基本概念 【理解】线程池工作原理 【掌握】自定义线程池 【应用】java内置线程池 【应用】使用java内置线程池完成综合案例 线程池 线程池基础 线程池使用 线程池综合案例 4.学员练习 5.线程池总结 概念介绍 1:什么是线程池 2:为什么使用线程池 3:线程池有哪些优势 什么是池 什么是线程池 线程池其实就是一种多线程处理形式,处理过程中可以将任务添加到队列中,然后在创建线程后自动启动这些任务。这里的线程就是我们前面学过的线程,这里的任务就是我们前面学过的实现了Runna.

拉格朗日的学习笔记 12万+

C++ 字符串相关算法总结

字符串相关的算法 字典树 何为字典树 在n个字符串中查找某个字符串,就像用英语字典查单词一样 先翻第一个字母,然后是第二个,然后是第三个 查找任意单词,查找次数最多只需要这个单词的字母个数 实现与功能 根节点不包含字符,除根节点以外每个子节点都只包含一个字符 从根节点到某一个节点,路径上的字符连接起来,为该节点对应的字符串 每个节点的所有子节点包含的字符互不相同 通常在结点设置一个标志来标记该节点...

qq_43826794的博客 908

015、PCIE带宽计算:理论vs实际——调试手记

最近在调一块图像采集卡,主控抱怨数据吞吐量上不去。规格书上写着PCIE 3.0 x4,理论带宽接近4GB/s,实际测试却卡在2.5GB/s左右。硬件同事拍胸脯说链路训练正常,软件同事咬定DMA配置没问题——这种场景是不是很熟悉?

z534437748的博客 883

字符串相关算法题

3. 无重复字符的最长子串

朱嫑膘的博客 316

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

你不懂的字符串相关算法都在这里!

真正搞透字符串常用办法,懂你的一些奇奇怪怪的疑惑点。

weixin_57140993的博客 2276

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

算法学习【3】字符串相关含KMP算法

面试经常会考察String相关问题,现把相关题目整理如下: 1、返回逆序字符串,如,"abc"->"cba",要求空间复杂度为O(1)      思路:首尾字符依次交换 public static char[] inverseOrder(char[] str, int st, int fh){ int len = fh-st+1; char temp; for(int i=0;i

tiandawangliang的专栏 701

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

18 道 Java 字符串相关算法题思路简介及代码实现

字符串(String)相关的问题占据着举足轻重的地位。这是因为字符串作为基本的数据类型之一,广泛应用于各类编程场景,包括但不限于数据传输、文件处理、用户界面展示、数据库操作、网络通信等。熟练掌握字符串操作不仅是日常编程工作的必备技能,也是评估面试者编程基础、逻辑思维和问题解决能力的重要依据

zp8126的专栏 1737

字符串相关操作、问题及算法提纲

这两天leetcode做完了三分之一,开始遇到瓶颈了,二叉树、链表

sdadssa1的专栏 384

字符串及相关算法

字符串的介绍及算法

swewing的博客 274

【LeetCode算法成长之路】Java字符串相关类总结与经典题目分析

本文小新为大家带来 Java字符串相关类总结与经典题目分析 相关知识,具体内容包括不可变字符序列String介绍(包括:String 的特性,构造器,String 与其他结构间的转换,基本常用方法,查找方法,字符串截取方法,开头与结尾判断方法,替换方法),可变字符序列StringBuffer与StringBuilder(包括:StringBuffer 与 StringBuilder 的理解,StringBuilder,StringBuffer 的 API)及相关经典题目进行详尽介绍~

小新要变强的博客 3168

【C++】字符串的基本操作和相关算法题

思路一、问题背景二、字符串的插入、删除和追加操作详解【C++】三、相关算法题【C++】 一、问题背景   首先来说字符串问题是十分经典的类型,我们经常会遇到输入是字符串的题目,然后利用各种STL标准库或者是一些动态规划的方法来求解问题,再或者是一些字符串遍历的一些技巧来提高效率等等,下边将介绍C++中相关的字符串常用操作函数以及我们可能常会遇到的算法问题。 二、字符串的插入、删除和追加操作详解【C++】 1. 字符串的插入(Insert函数) 1)在字符串s1的第p(从0开始数)个位置前插入字符串s2:s1

郝同学的博客 2469

String字符串相关算法

例一 题目描述: 判断字符串里括号是否成对出现 import java.util.HashMap; import java.util.Map; import java.util.Stack; public class matchJudger { // pair以右括号为key, 左括号为值 private Map<Character, Character> pa...

weixin_44212308的博客 260

有关字符串的相关算法

说明: 这篇博客是自己在学习的时候,遇到的比较好的,特此借鉴来学习,整理留存,方便学习之用。如有侵权之处,可以联系我删除,感谢牛人的分享。 学习: 字符串匹配的三个算法(KMP+字典树+AC自动机) AC自动机 AC自动机 算法详解(图解)及模板 AC自动机算法详解 (转载) Trie 树 小白详解 Trie 树 ...

wchzh2015的博客 323

数组与字符串相关的算法

最近在看 《程序员面试金典》,感觉书中的一些算法非常的精妙,所以本人对书中的每一个题目都着手实现了一遍,为了以后能够方便的重温这个知识,也为了能够将这个算法分享给 没看过这本书的小伙伴,于是本人决定将这些题目和算法写下来,当然我也只是写下题目和解法,不会对解法进行分析,想看具体分析可以买一本纸质书看一下,非常不错的一本书。1.1 实现一个算法,确定一个字符串的所有字符是否全都不同。假设不允许使用额外

Hwaphon 3438
上一篇: 队列的应用题目
下一篇: 字符串判断子串
蓝齐儿
博客等级 码龄17年 6粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值