พิจารณาว่าเรามีสตริงและข้อความค้นหาบางรายการในชุด Q แต่ละข้อความค้นหาประกอบด้วยจำนวนเต็ม i และ j และอีกตัว c. เราต้องแทนที่อักขระที่ดัชนี i และ j ด้วยอักขระใหม่ c และบอกได้ว่าสตริงนั้นเป็นพาลินโดรมหรือไม่ สมมติว่าสตริงเป็นเหมือน "AXCDCMP" หากเราใช้หนึ่งแบบสอบถามเช่น (1, 5, B) สตริงจะเป็น "ABCDCBP" จากนั้นแบบสอบถามอื่นเช่น (0, 6, A) จะเป็น "ABCDCBA ” นี่คือพาลินโดรม
เราต้องสร้างหนึ่งแบบสอบถามโดยใช้ดัชนี i, j จากนั้นแทนที่อักขระที่มีอยู่ในดัชนี i, j ในสตริงด้วย c.
ตัวอย่าง
#include <iostream>
using namespace std;
class Query{
public:
int i, j;
char c;
Query(int i, int j, char c){
this->i = i;
this->j = j;
this->c = c;
}
};
bool isPalindrome(string str){
int n = str.length();
for (int i = 0; i < n/2 ; i++)
if (str[i] != str[n-1-i])
return false;
return true;
}
bool palindromeAfterQuerying(string str, Query q[], int n){
for(int i = 0; i<n; i++){
str[q[i].i] = q[i].c;
str[q[i].j] = q[i].c;
if(isPalindrome(str)){
cout << str << " is Palindrome"<< endl;
}else{
cout << str << " is not Palindrome"<< endl;
}
}
}
int main() {
Query q[] = {{1, 5, 'B'}, {0, 6, 'A'}};
int n = 2;
string str = "AXCDCMP";
palindromeAfterQuerying(str, q, n);
} ผลลัพธ์
ABCDCBP is not Palindrome ABCDCBA is Palindrome