C程序查找形成回文的最小插入次数

c程序查找形成回文的最小插入次数

回文是一个与其反转相等的字符串。给定一个字符串,我们需要找到使该字符串成为回文所需的最小插入任意字符的数量。我们将看到三种方法:首先是递归方法,然后我们将记忆化这个解决方案,最后,我们将实现动态规划方法。

递归方法

示例

#include  // library for input and output#include  // library to get the integer limits #include  // 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  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  // library for input and output#include  // library to get the integer limits #include  // 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  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  // library for input and output#include  // library to get the integer limits #include  // 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程序查找形成回文的最小插入次数的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:31:31
下一篇 2025年12月8日 10:57:12

相关推荐

  • 在C程序中的Windows线程API

    线程是在 Windows API 中使用 CreateThread() 函数创建的,并且就像在 Pthreads 中一样,安全信息、堆栈大小和线程标志等一组属性将传递给该函数。在下面的程序中,我们使用这些属性的默认值。 (默认值最初不会将线程设置为挂起状态,而是使其有资格由 CPU 调度程序运行。)…

    2025年12月17日
    000
  • C程序在数组中找到最小和最大的质数

    问题陈述 给定一个包含 n 个正整数的数组。我们必须找到素数具有最小值和最大值的数字。 如果给定的数组是 – arr [] = {10, 4, 1, 12, 13, 7, 6, 2, 27, 33}then minimum prime number is 2 and maximum pr…

    2025年12月17日
    000
  • 在C程序中使用递归函数的辅助空间?

    这里我们将看到递归函数调用如何需要辅助空间。它与普通函数调用有何不同? 假设我们有一个如下所示的函数 – long fact(int n){ if(n == 0 || n == 1) return 1; return n * fact(n-1);} 该函数是递归函数。当我们像fact(5…

    2025年12月17日
    000
  • 在C程序中,将句子中最长的回文单词打印出来

    给定一个句子,挑战是从给定的句子中找到最长的回文 什么是回文? 回文是一个单词或序列,即使在之后其含义仍然保持不变反转字符串 示例 – Nitin,反转字符串后其含义保持不变。 挑战是从给定的句子中找到最长的回文。 喜欢的句子是:malayalam liemadameil iji 它包含…

    2025年12月17日
    000
  • C++程序将一个数组推入另一个数组中

    A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain fea…

    2025年12月17日
    000
  • 编写一个C程序,使用elseif语句将数字打印为单词

    问题 在不使用 switch case 的情况下,如何使用 C 编程语言以文字形式打印给定的数字? 解决方案 在此程序中,我们检查三个条件以用单词打印两位数 – if(no99) if(no99) p> 输入的数字不是两位数 else if(no==0) 将第一个数字打印为零 el…

    2025年12月17日
    000
  • C++程序以给定值为参数,寻找双曲反余弦的函数

    类似于普通三角函数,双曲函数是使用双曲线而不是圆来定义的。从指定的弧度角度,它返回双曲余弦函数中的比值参数。但换句话说,它是相反的。需要使用反双曲三角运算(如反双曲余弦运算)来确定双曲余弦值对应的角度。 使用双曲余弦值计算角度,以弧度为单位,本教程将展示如何使用C++双曲反余弦(acosh)函数。双…

    2025年12月17日
    000
  • 基数排序的C程序

    排序算法是一种按特定顺序排列列表组件的算法。最常用的顺序是数字顺序和字典顺序。 基数排序是一种非比较排序算法。基数排序算法是未排序列表的首选算法。 它通过最初对相同位值的各个数字进行分组来对元素进行排序。基数排序的思想是按照递增/递减顺序从最低有效数字(LSD)到最高有效数字(MSD)进行逐位排序。…

    2025年12月17日
    000
  • 给定一个数字,编写一个C程序来找到斐波那契数列

    斐波那契数列是通过将前两个数字相加得到的一系列数字。 斐波那契数列从两个数字f0和f1开始。 fo和f1的初始值可以取0、1或1、1。 Fibonacci序列满足以下条件: fn = fn-1 + fn-2 算法 参考Fibonacci序列的算法。 STARTStep 1: Read integer…

    2025年12月17日
    000
  • 编写一个C程序,将给定的天数转换为年、周和天

    给定了天数,任务是将给定的天数转换为年、周和天。 让我们假设一年中的天数 =365 年数=(天数)/365 解释-:年数将是除以给定天数得到的商与 365 周数 = (天数 % 365) / 7 解释-:周数将通过收集余数获得将天数除以 365,再除以一周的天数 7。 天数 = (天数 % 365)…

    2025年12月17日
    000
  • 在C程序中,从给定的数组中打印下三角矩阵模式

    给定一个 n x n 的矩阵,任务是以下三角形式打印出该矩阵。 下三角矩阵是一个矩阵,其主对角线以下的元素包括主对角线元素,其余元素均为零。 我们通过以下图示来理解: 上述绿色元素是主对角线以下的元素,红色元素是主对角线以上的元素,它们被设为零。 示例 Input: matrix[3][3] = {…

    2025年12月17日
    000
  • 给定输入的C程序,移除括号

    问题 让我们通过删除表达式中的括号来创建一个简化的表达式。 解决方案 示例 1 Input: A string expression with bracket is as follows:(x+y)+(z+q)The output is as follows:x+y+z+q 示例 2 The inp…

    2025年12月17日
    000
  • C++程序遍历字典

    虽然C++没有字典,但它有一种类似字典的结构,称为map。每个map的条目中包含两个值−键和映射值−。每个项目都使用键值进行索引,而映射值是与键相关联的值。映射值可能是唯一的,也可能不是唯一的,但键始终是唯一的。在本教程中,我们将看一下迭代器以及它们如何与map一起工作。 在C++中的迭代器 迭代器…

    2025年12月17日
    000
  • C++程序以给定弧度值找到双曲余弦值

    双曲函数是使用双曲线而不是圆定义的,与普通三角函数相当。双曲函数在双曲几何中用于计算角度和距离。它们还出现在大量线性微分方程、三次方程等的解中。对于给定的角度$theta$。双曲余弦函数 cosh$(theta)$ 如下 $$mathrm{cos(x):=:frac{e^x:+:e^{-x}}{2}…

    2025年12月17日
    000
  • C程序:两个分数相加

    给定输入为分数,即 a/b 和 c/d,其中 a、b、c 和 d 可以是除 0 以外的任何整数值,任务是将这两个分数相加以生成它们的最终和。 分数用 − 表示 a / b,其中 a 被称为分子,b 被称为分母。a 和 b 可以有任何数值,但 b 不能为 0。两个分数的和表示为 a / b + c /…

    2025年12月17日
    000
  • C++程序用于找到给定矩阵的迹和法线

    一些应用程序可以从二维数组或矩阵的使用中受益匪浅。数字存储在矩阵的行和列中。使用多维数组,我们也可以用 C++ 定义 2D 矩阵。在这篇文章中,我们将了解如何使用 C++确定给定矩阵的法线和迹线。 矩阵中元素总数的平方根就是所谓的普通的。迹线由构成主对角线的所有组件组成。让我们查看 C++ 代码中算…

    2025年12月17日
    000
  • 使用结构体编写的C程序,用于计算圆和圆柱体的面积

    在C编程语言中,我们可以利用结构体来找到圆的面积、圆柱体的面积和体积。 用于找到圆的面积的逻辑如下: s.areacircle = (float)pi*s.radius*s.radius; 用于计算圆柱体的面积的逻辑如下: s.areacylinder = (float)2*pi*s.radius*…

    2025年12月17日
    000
  • C程序打印所有ASCII值

    问题 打印 0 到 255 个字符的美国信息交换标准代码 (ASCII) 值,而不将字符初始化为整数类型变量。只需使用格式说明符即可。 解决方案 这里我们编写一个程序,仅打印 65 到 122。 如果您想查看所有 ASCII值,在 for 循环中你可以写如下 – For(i=0;i&lt…

    2025年12月17日
    000
  • 在C程序中,将一个数组中具有最大AND值的一对元素打印出来

    根据问题,我们给定了一个包含n个正整数的数组,我们需要从数组中找到具有最大AND值的一对。 示例 Input: arr[] = { 4, 8, 12, 16 }Output: pair = 8 12The maximum and value= 8Input:arr[] = { 4, 8, 16, 2…

    2025年12月17日
    000
  • C程序用于矩阵相减

    给定两个矩阵 mat1[行][列] 和 mat2[行][列],我们必须找到两个矩阵之间的差异并打印两个矩阵相减后获得的结果。两个矩阵相减为 mat1[n][m] – mat2[n][m]。 对于减法,两个矩阵的行数和列数应该相同。 示例 Input:MAT1[N][N] = { {1, 2, 3},…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信