首頁 > 後端開發 > C++ > 主體

C程式找出形成回文的最小插入次數

WBOY
發布: 2023-09-05 17:13:05
轉載
1277 人瀏覽過

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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!