
为您提供了一个长度为 n 的字符串 str。打印字符串中每个元素的位置,以便它可以形成回文,否则在屏幕上打印消息“No palindrome”。
什么是回文?
Palindrome 是一个单词,从反向或向后读取的字符序列与从正向读取的字符序列相同,例如 MADAM、racecar。
要查找序列或单词是回文,我们通常将单词的反向存储在单独的字符串中并比较两者,如果它们相同,则给定的单词或序列是回文。但是在这个问题中,我们必须打印排列以形成回文中的单词或序列。
就像,有一个字符串 str = “tinni” 那么它可以是 intni 或 nitin 所以我们必须返回作为从 1 开始的索引和结果的任何一个排列顺序可以是 2 3 1 4 5 或 3 2 1 5 4 两者中的一个。
上述问题需要像下面给出的示例那样的解决方案-
示例
Input: string str = “baa” Output: 2 1 3 Input: string str = “tinni” Output: 2 3 1 4 5
算法
void printPalindromePos(string &str)
START
STEP 1: DECLARE vector<int> pos[MAX]
STEP 2: DECLARE AND ASSIGN n WITH LENGTH OF str
STEP 3: LOOP FOR i = 0 AND i 1 THEN,
PRINT "NO PALINDROME"
STEP 8: LOOP FOR i=0 AND i<max and i mid="pos[i].size()/2" for j="0" print pos loop end step if oddcount> 0 THEN,
DECLARE AND SET last = pos[oddChar].size() - 1
PRINT pos[oddChar][last]
SET pos[oddChar].pop_back();
END IF
STEP 10: LOOP FOR i=MAX-1 AND i>=0 AND i--
DECLARE AND SET count = pos[i].size()
LOOP FOR j=count/2 AND j<count and j print pos stop><h2>示例</h2>
<pre class="brush:php;toolbar:false;">#include <bits>
using namespace std;
// Giving the maximum characters
const int MAX = 256;
void printPalindromePos(string &str){
//Inserting all positions of characters in the given string.
vector<int> pos[MAX];
int n = str.length();
for (int i = 0; i 1)
cout 0){
int last = pos[oddChar].size() - 1;
cout =0; i--){
int count = pos[i].size();
for (int j=count/2; j<count j cout pos int main s="tinni"><h2>输出</h2>
<p>如果我们运行上面的程序,那么它将生成以下输出 -</p>
<pre class="brush:php;toolbar:false;">2 3 1 4 5











