僅交換一個字元即可構成迴文


在C++環境中,迴文是指在程序的初始階段到該特定程序終止的過程中,集合或字串保持不變的過程。假設我們有一個字串,表示為str[]。任務是檢查在僅執行一次字元交換後,字串是否為迴文。

這是一個交換過程的通用示例:

Input : ARBRDD
Output : true
Explanation: Swap the value A(1st index present in the list) with the R.
Input : INDBAN
Output : true
Explanation: Swap the vlaue of I(0th element present in the index set) with
N(last value of the index here) or
Swap I(1st value present in the index list) with N(the second
value present in the index list)
Input : gcagac
Output : the value is totally false

僅交換一個字元即可找到迴文的演算法

在這個可能的演算法中,我們將透過在C++環境中僅交換一個字元來執行迴文的列印過程。透過此演算法,我們將構建一些C++語法,以有效的方式學習問題陳述。

  • 步驟1 - 開始程序。

  • 步驟2 - 宣告輸入輸出流。

  • 步驟3 - 匯入內建類和宣告的函式。

  • 步驟4 - 宣告輸入長度。

  • 步驟5 - 防止字串成為迴文。

  • 步驟6 - 計算差值。

  • 步驟7 - 記錄那些阻止程序的字元。

  • 步驟8 - 迴圈到中點。

  • 步驟9 - 記錄和儲存不同的字元。

  • 步驟10 - 開啟開關。

  • 步驟11 - 如果找到迴文值,則返回true。

  • 步驟12 - 否則返回false作為表示。

  • 步驟13 - 查詢中間字元的差異。

  • 步驟14 - 宣告驅動程式程式碼並獲取返回值。

  • 步驟15 - 終止程序。

僅交換一個字元即可找到迴文的語法

String input = "INPUT STRING";
char[] arr = input.toCharArray();
int swap = 0;
int i = 0;
int j = arr.length-1;
char temp;
while(i<j){
if(arr[i] != arr[j]){
   if(arr[i+1] == arr[j]){
		temp = arr[i];
		arr[i] = arr[i+1];
		arr[i+1] = temp;
		i++;j--;
		swap++;
   } else if(arr[i] == arr[j-1]){
		temp = arr[j];
		arr[j] = arr[j-1];
		arr[j-1] = temp;
		i++;j--;
		swap++;
	} else if(arr[i+1] == arr[j-1] && i+1 != j-1){
		temp = arr[j];
		arr[j] = arr[j-1];
		arr[j-1] = temp;
		temp = arr[i];
		arr[i] = arr[i+1];
		arr[i+1] = temp;
		i++;j--;
		swap = swap+2;
	} else {
		swap = -1;break;
	}
}
else{
   i++;j--;
}

在這個可能的語法中,我們已經透過在C++環境中僅交換一個字元來執行迴文的列印過程。透過這些可能的語法,我們將構建一些C++語法,以更廣泛的方式學習問題陳述。

遵循的方法

  • 方法1 - 使用bool isPalindromePossible()、str.length()和雙指標方法提取僅交換一個字元的迴文值的C++程式

  • 方法2 - 使用詞典值和迭代方法提取僅交換一個字元的迴文值的C++程式

方法1:使用Bool IsPalindromePossible()、Str.length()和雙指標方法

Bool IsPalindromePossible()方法的使用

在這種可能的方法中,我們將應用布林isPalindromePossible()方法,透過從特定字串中交換一個字元來列印迴文值。

bool check(string s){
	int n=s.length();
	map<char,int> m;
	for(auto i:s){
		m[i]++;
	}
	int cnt=0;
	for(auto i=m.begin();i!=m.end();i++)
	128{
		if(i->second%2){
			cnt++;
		}
	}
	if(n%2&&cnt==1){return true;}
	if(!(n%2)&&cnt==0){return true;}
	return false;
}

示例

//C++ program palindrome by swapping only one character with the bool isPalindromePossible() method
#include <bits/stdc++.h>
using namespace std;
bool isPalindromePossible(string input){
   int len = input.length();
   int diffCount = 0, i;
   char diff[2][2];
   for (i = 0; i < len / 2; i++){
      if (input[i] != input[len - i - 1]){
         if (diffCount == 2) return false;
         diff[diffCount][0] = input[i];
         diff[diffCount++][1] = input[len - i - 1];
      }
   }
   switch (diffCount){
      case 0:
      return true;
      case 1:{
         char midChar = input[i];
         if (len % 2 != 0 and
         (diff[0][0] == midChar or
         diff[0][1] == midChar))
         return true;
      }
      case 2:
      if ((diff[0][0] == diff[1][0] and
      diff[0][1] == diff[1][1]) or
      (diff[0][0] == diff[1][1] and
      diff[0][1] == diff[1][0]))
      return true;
   }
   return false;
}
int main(){
   cout << boolalpha
   << isPalindromePossible("RDD") << endl;
   cout << boolalpha
   << isPalindromePossible("ARBRDD") << endl;
   cout << boolalpha
   << isPalindromePossible("INDBDKOLDHAKA") << endl;
   return 0;
}

輸出

true
false
false

Str.length()方法的使用

在這種可能的方法中,我們將應用str.length()方法,透過從特定字串中交換一個字元來列印迴文值。

示例

//C++ program palindrome by swapping only one character with the possible method to convert the string into palindrome string by changing only one character
#include<bits/stdc++.h>
using namespace std;
bool checkPalindrome(string str){
   int n = str.length();
   int count = 0;
   for (int i = 0; i < n/2; ++i)
   if (str[i] != str[n - i - 1])
   ++count;
   return (count <= 1);
}
int main(){
   string str = "ARBRDDINDDBDKOLKATADHAKA";
   if (checkPalindrome(str))
   cout << "Yes - The Process Done " << endl;
   else
   cout << "No - The Process Is Not Done" << endl;
   return 0;
}

輸出

No - The Process Is Not Done

雙指標方法的使用

在這種可能的方法中,我們將應用使用兩個指標的方法,透過從特定字串中交換一個字元來列印迴文值。

示例

//C++ program palindrome by swapping only one character with the possible method by using two pointer approach
#include <bits/stdc++.h>
using namespace std;
int countSwap(string s){
   int n = s.length();
   int count = 0;
   for (int i = 0; i < n / 2; i++){
      int left = i;
      int right = n - left - 1;
      while (left < right){
         if (s[left] == s[right]){
            break;
         } else{
            right--;
         }
      }
      if (left == right){
         return -1;
      }
      for (int j = right; j < n - left - 1; j++){
         swap(s[j], s[j + 1]);
         count++;
      }
   }
   return count;
}
int main(){
   string s = "ARBRDDKOLDHKAINDBD";
   int ans1 = countSwap(s);
   reverse(s.begin(), s.end());
   int ans2 = countSwap(s);
   cout << max(ans1, ans2);
   return 0;
}

輸出

-1

方法2:使用詞典值和迭代方法

詞典方法的使用

詞典比較是一個比較函式,它接受兩個引數進行處理。在這種可能的方法中,我們將應用詞典方法,透過從特定字串中交換一個字元來列印迴文值。

int main(){
string a;
while(cin>>a){
if(a[0]=='0'){
	break;
}
string s;s=a;
int n=s.length();
int cnt=0;
bool ini=false;
if(n%2){ini=true;}
if(check(s)){
	for(int i=0;i<n/2;i++){
		bool fl=false;
		int j=0;
			for(j=n-1-i;j>i;j--){
			if(s[j]==s[i]){
				fl=true;
				for(int k=j;k<n-1-i;k++){
					swap(s[k],s[k+1]);
					cnt++;
					cout<<cnt<<endl<<flush;
				}
				cout<<" "<<i<<" "<<cnt<<endl<<flush;
				break;
			}
		}
		if(!fl&&ini){
			for(int k=i;k<n/2;k++){
				swap(s[k],s[k+1]);
				cnt++;
			}
			cout<<cnt<<" "<<i<<" "<<endl<<flush;
		}
	}
	cout<<cnt<<endl;
}
else{
cout<<"OUTPUT NOTATION PROCESS"<<endl;

示例

//C++ program check whether is it possible to make string A lexicographically smaller than string B to perform the palindrome swaping with one character
#include <bits/stdc++.h>
using namespace std;
void swap(char& x, char& y){
   char temp = x;
   x = y;
   y = temp;
}
bool IsLexicographicallySmaller(
string A, string B){
   if (A < B){
      return true;
   }
   string temp = A;
   sort(temp.begin(), temp.end());
   int index = -1;
   for (int i = 0; i < A.length(); i++){
      if (A[i] != temp[i]) {
         index = i;
         break;
      }
   }
   if (index == -1){
      return false;
   }
   int j;
   for (int i = 0; i < A.length(); i++){
      if (A[i] == temp[index])
      j = i;
   }
   swap(A[index], A[j]);
   if (A < B){
      return true;
   } else {
      return false;
   }
}
int main(){
   string A = "ABONI DAS";
   string B = "RUDRA BISWAS";
   if (IsLexicographicallySmaller(A, B)) {
      cout << "Yes"
      << "\n";
   } else {
      cout << "No"
      << "\n";
   }
   return 0;
}

輸出

Yes

迭代方法的使用

在這種可能的方法中,我們將應用迭代方法來列印透過從特定字串迴圈中(0, N/2)範圍內交換一個字元而獲得的迴文值。在這裡,我們將移除或忽略字元c後,執行一個字元的迴文交換。

示例

//C++ program to perform the palindrome swaping with one character after removal or neglecting character c and iterate a loop in the range of (0, N/2)
#include <bits/stdc++.h>
using namespace std;
bool check_palindrome(string str, char c){
   int n = str.length(), i = 0, j = n - 1;
   while (i < j) {
      if (str[i] == c)
      i++;
      else if (str[j] == c)
      j--;
      else if (str[i] != str[j])
      return 0;
      else
      i++, j--;
   }
   return 1;
}
string make_palindrome(string str){
   bool is_palindrome = 1;
   int n = str.length();
   if (n == 1 || n == 2) {
      return "YES";
   }
   for (int i = 0; i < n / 2; ++i) {
      if (str[i] != str[n - 1 - i]) {
         is_palindrome
         = check_palindrome(str, str[i])
         || check_palindrome(
         str, str[n - 1 - i]);
         break;
      }
   }
   if (is_palindrome)
   return "Yes";
   else
   return "No";
}
int main(){
   string S = "ARBRDDKOLKATADHAKAINDBANGLADESH";
   string res = make_palindrome(S);
   cout << (res);
   return 0;
}

輸出

No

結論

在今天的文章中,我們學習瞭如何在C++環境中實現構建和應用各種方法來列印透過僅交換一個字元設定的迴文值集的過程。透過上述邏輯、語法和演算法;我們嘗試構建一些C++程式碼來有效地解決問題陳述。

更新於:2023年12月27日

127 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告