查詢Palindrome字串的C++程式碼,其子字串為S
假設我們有一個包含n個字母的字串S。我們必須找出另一個字串T,使得T是迴文串且S是T的子序列。
因此,如果輸入是S="ab",則輸出將是"aabaa"(還有其他答案可用)
步驟
要解決這個問題,我們將遵循以下步驟-
res := S reverse the array S res := res + S return res
示例
讓我們看看以下實現,以便更好地理解-
#include <bits/stdc++.h>
using namespace std;
string solve(string S){
string res = S;
reverse(S.begin(), S.end());
res += S;
return res;
}
int main(){
string S = "ab";
cout << solve(S) << endl;
}輸入
ab
輸出
abba
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP