program XauDoiXung;
uses
SysUtils;
var
inputStr, resultStr: string;
found: Boolean;
function IsPalindrome(s: string): Boolean;
var
i, j: Integer;
begin
j := Length(s);
for i := 1 to Length(s) div 2 do
begin
if s[i] <> s[j] then
begin
IsPalindrome := False;
Exit;
end;
Dec(j);
end;
IsPalindrome := True;
end;
function NextChar(c: Char): Char;
begin
if c = '?' then
Result := 'A'
else if c = 'Z' then
Result := '?'
else
Result := Succ(c);
end;
procedure SolvePalindrome;
var
i: Integer;
begin
Write('Nhap xau: ');
ReadLn(inputStr);
found := False;
repeat
resultStr := inputStr;
for i := 1 to Length(inputStr) do
begin
if inputStr[i] = '?' then
resultStr[i] := 'A'
else
resultStr[i] := inputStr[i];
end;
if IsPalindrome(resultStr) then
begin
found := True;
WriteLn('Xau doi xung: ', resultStr);
end;
inputStr := resultStr;
i := Length(inputStr);
while (i > 0) and (inputStr[i] = 'Z') do
begin
inputStr[i] := '?';
Dec(i);
end;
if i > 0 then
inputStr[i] := NextChar(inputStr[i]);
until (i = 0);
if not found then
WriteLn('XAU KHONG DOI XUNG');
end;
begin
SolvePalindrome;
end.