C程序查找形成回文的最小插入次数
回文是一个与其反转相等的字符串。给定一个字符串,我们需要找到使该字符串成为回文所需的最小插入任意字符的数量。我们将看到三种方法:首先是递归方法,然后我们将记忆化这个解决方案,最后,我们将实现动态规划方法。
递归方法
示例
#include <stdio.h> // library for input and output #include <limits.h> // library to get the integer limits #include <string.h> // library for strings // function to find the minimum of two number // as it is not present in the c language int findMin(int a, int b){ if(a < b){ return a; } else{ return b; } } // creating the function to find the required answer we will make recursive calls to it int findAns(char str[], int start, int end){ // base condition if (start > end){ return INT_MAX; } else if(start == end){ return 0; } else if (start == end - 1){ if(str[start] == str[end]){ return 0; } else return 1; } // check if both start and end characters are the same make callson the basis of that if(str[start] == str[end]){ return findAns(str,start+1, end-1); } else{ return 1+ findMin(findAns(str,start,end-1), findAns(str,start+1,end)); } } // main function int main(){ char str[] = "thisisthestring"; // given string printf("The minimum number of insertions required to form the palindrome is: %d", findAns(str,0,strlen(str)-1)); return 0; }
输出
The minimum number of insertions required to form the palindrome is: 8
时间和空间复杂度
以上代码的时间复杂度为O(2^N),因为我们对每个插入都进行选择,其中N是给定字符串的大小。
上述代码的空间复杂度为O(N),即在递归调用中使用。
记忆方法
例子
#include <stdio.h> // library for input and output #include <limits.h> // library to get the integer limits #include <string.h> // library for strings int memo[1005][1005]; // array to store the recursion results // function to find the minimum of two number // as it is not present in the c language int findMin(int a, int b){ if(a < b){ return a; } else{ return b; } } // creating the function to find the required answer we will make recursive calls to it int findAns(char str[], int start, int end){ // base condition if (start > end){ return INT_MAX; } else if(start == end){ return 0; } else if (start == end - 1){ if(str[start] == str[end]){ return 0; } else return 1; } // if already have the result if(memo[start][end] != -1){ return memo[start][end]; } // check if both start and end characters are same make calls on basis of that if(str[start] == str[end]){ memo[start][end] = findAns(str,start+1, end-1); } else{ memo[start][end] = 1+ findMin(findAns(str,start,end-1), findAns(str,start+1,end)); } return memo[start][end]; } int main(){ char str[] = "thisisthestring"; // given string //Initializing the memo array memset(memo,-1,sizeof(memo)); printf("The minimum number of insertions required to form the palindrome is: %d", findAns(str,0,strlen(str)-1)); return 0; }
输出
The minimum number of insertions required to form the palindrome is: 8
时间和空间复杂度
上述代码的时间复杂度为O(N^2),因为我们存储了已经计算过的结果。
上述代码的空间复杂度为O(N^2),因为我们在这里使用了额外的空间。
动态规划方法
示例
#include <stdio.h> // library for input and output #include <limits.h> // library to get the integer limits #include <string.h> // library for strings // function to find the minimum of two number // as it is not present in the c language int findMin(int a, int b){ if(a < b){ return a; } else{ return b; } } // creating a function to find the required answer int findAns(char str[], int len){ // creating the table and initialzing it int memo[1005][1005]; memset(memo,0,sizeof(memo)); // filling the table by traversing over the string for (int i = 1; i < len; i++){ for (int start= 0, end = i; end < len; start++, end++){ if(str[start] == str[end]){ memo[start][end] = memo[start+1][end-1]; } else{ memo[start][end] = 1 + findMin(memo[start][end-1], memo[start+1][end]); } } } // return the minimum numbers of interstion required for the complete string return memo[0][len-1]; } int main(){ char str[] = "thisisthestring"; // given string // calling to the function printf("The minimum number of insertions required to form the palindrome is: %d", findAns(str, strlen(str))); return 0; }
输出
The minimum number of insertions required to form the palindrome is: 8
时间和空间复杂度
上述代码的时间复杂度为O(N^2),因为我们在这里使用了嵌套的for循环。
上述代码的空间复杂度为O(N^2),因为我们在这里使用了额外的空间。
结论
在本教程中,我们实现了三种方法来找到使给定字符串成为回文所需的最小插入次数。我们实现了递归方法,然后进行了记忆化处理。最后,我们实现了表格法或动态规划法。
以上是C程序查找形成回文的最小插入次数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

给出以下是一个将罗马数字转换为十进制数字的C语言算法:算法步骤1-开始步骤2-在运行时读取罗马数字步骤3-长度:=strlen(roman)步骤4-对于i=0到长度-1 步骤4.1-switch(roman[i]) 步骤4.1.1-case'm': &nbs

字典序字符串比较是指字符串按照字典顺序进行比较。例如,如果有两个字符串'apple'和'appeal',第一个字符串将排在后面,因为前三个字符'app'是相同的。然后对于第一个字符串,字符是'l',而在第二个字符串中,第四个字符是'e'。由于'e'比'l'短,所以如果我们按照字典顺序排列,它将排在前面。在安排之前,字符串按字典顺序进行比较。在本文中,我们将看到使用C++进行按字典顺序比较两个字符串的不同技术。在C++字符串中使用compare()函数C++string对象有一个compare()

双曲函数是使用双曲线而不是圆定义的,与普通三角函数相当。它从提供的弧度角返回双曲正弦函数中的比率参数。但要做相反的事,或者换句话说。如果我们想根据双曲正弦值计算角度,我们需要像双曲反正弦运算一样的反双曲三角运算。本课程将演示如何使用C++中的双曲反正弦(asinh)函数,使用双曲正弦值(以弧度为单位)计算角度。双曲反正弦运算遵循以下公式-$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})},其中\:In\:是\:自然对数\:(log_e\:k)

链接列表使用动态内存分配,即它们相应地增长和收缩。它们被定义为节点的集合。这里,节点有两部分,即数据和链路。数据、链接和链表的表示如下-链表的类型链表有四种类型,如下:-单链表/单链表双/双向链表循环单链表循环双链表我们使用递归方法求链表长度的逻辑是-intlength(node*temp){ if(temp==NULL) returnl; else{&n

映射是C++中的一种特殊类型的容器,其中每个元素都是一对两个值,即键值和映射值。键值用于索引每个项目,映射值是与键关联的值。无论映射值是否唯一,键始终是唯一的。要在C++中打印映射元素,我们必须使用迭代器。一组项目中的一个元素由迭代器对象指示。迭代器主要与数组和其他类型的容器(例如向量)一起使用,并且它们具有一组特定的操作,可用于识别特定范围内的特定元素。可以增加或减少迭代器来引用范围或容器中存在的不同元素。迭代器指向范围内特定元素的内存位置。使用迭代器在C++中打印地图首先,我们看一下如何定义

rename函数将文件或目录从旧名称更改为新名称。此操作类似于移动操作。因此,我们也可以使用此rename函数来移动文件。此函数存在于stdio.h库头文件中。rename函数的语法如下:intrename(constchar*oldname,constchar*newname);rename()函数的功能它接受两个参数。一个是oldname,另一个是newname。这两个参数都是指向常量字符的指针,用于定义文件的旧名称和新名称。如果文件重命名成功,则返回零;否则,返回非零整数。在重命名操作期间

现代科学在很大程度上依赖于复数的概念,这一概念最初是通过GirolamoCardano在16世纪引入的在17世纪初建立。复数的公式是a+ib,其中a保留html代码并且b是实数。一个复数被认为有两个部分:实部<a>和虚部(<ib>)。i或iota的值为√-1。C++中的复数类是一个用于表示复数的类。C++中的complex类可以表示并控制几个复数操作。我们来看一下如何表示和控制显示复数。imag()成员函数如前所述,复数由实部和虚部两部分组成。显示实部我们使用real()

在解决一些逻辑编程问题时,使用字符串或字符有时非常有用。字符串是字符的集合,字符是1字节数据类型,用于保存ASCII值中的符号。符号可以是英文字母、数字或特殊字符。在本文中,我们将学习如何使用C++检查一个字符是否是英文字母或字母表中的字母。检查isalpha()函数要检查数字是否是字母,我们可以使用ctype.h头文件中的isalpha()函数。这将一个字符作为输入,如果是字母表,则返回true,否则返回false。让我们看看下面的C++实现来了解这个函数的用法。Example的中文翻译为:示
