47

# 例子：

800233008   -> 2
racecarFOOL -> FOOL
abcdedcba   -> (empty string)
ngryL Myrgn -> "L " (or " M")
123456789   -> 12345678 (or 23456789)
aabcdbaa    -> c (or d)
[[]]        -> [[ (or ]])
a           -> (empty string)

aabaab      -> b    | Suggested by Zgarb, some returned "aa".

# 规则

• 输入中仅会显示可打印的ASCII字符（没有换行符，请保持简单）。
• 不是一个真正的规则，但要注意<>/\()[]{}不是回文。

3
Tesf案：aabaab
Zgarb

14

ShreevatsaR

4
@Carl看起来像一个，但是当您反转字符时，您会得到]][[。考虑那aabb是同一件事，只是字符不同。

1
（将获得7/12的奖励） “是吗？

8

# 果冻，16字节

Ḣ;Ṫµ=Ṛ
0,0jŒṖÇÞṪ

### 这个怎么运作

0,0jŒṖÇÞṪ  Main link. Argument: s (string)

0,0j       Join [0, 0], separating by s. This prepends and appends a 0 to s.
ŒṖ     Build all partitions of the resulting array.
ÇÞ   Sort the partitions by the helper link.
As a side effect, this will remove the first and last element of each
partition. The 0's make sure that not removing any characters from s
will still remove [0] from both sides.
Ṫ  Tail; extract the last one.

Ḣ;Ṫµ=Ṛ     Helper link. Argument: A (array/partition)

Ḣ          Head; yield and remove the first chunk of A.
Ṫ        Tail; yield and remove the last chunk of A.
µ=Ṛ     Compare the result, character by character, with its reverse.
A palindrome of length l will yield an array of l 1's, while a
non-palindrome of length l will yield an array with at least one 0 among
the first l/2 Booleans. The lexicographically largest result is the one
with the longest prefix of 1's, which corresponds to the longest
palindrome among the outfixes.

10

# J，24个字节

(0{::(-:|.)\.#&,<\)~i.@#

## 说明

(0{::(-:|.)\.#&,<\)~i.@#  Input: array of chars S
#  Length of S
i.@   Range, [0, 1, ..., len(S)-1]
(                 )~      Dyadic verb on range and S
\.               For each outfix of S of size x in range
|.                    Reverse
-:                      Matches input (is palindrome)
<\          Box each infix of S of size x in range
#&,            Flatten each and copy the ones that match
0{::                       Fetch the result and index 0 and return

7

## Wolfram语言（Mathematica），53 51字节

±{a___,Shortest@b___,c___}/;PalindromeQ[a<>c]:={b}

Reverse然后比较这扭转原来的比PalindromeQ短？我不认识Mathematica，所以不知道。

@MagicOctopusUrn Reverse[x={a,c}]==x长两个字节。我不知道是否还有更短的选择。

@KellyLowder字符列表是PPCG上字符串的有效表示形式。在Mathematica中，这有点尴尬，您通常不使用该表示法，但是它仍然有效。我将挖掘一个元文章。

1
@KellyLowder我认为这是公认的政策。Mathematica之所以尴尬的主要原因是Mathematica没有实际的字符类型，因此这些字符最终都是单例字符串。

5

# 05AB1E，18个字节

ā<Œ¯¸«ʒRõsǝÂQ}éнèJ

ǝ真是个天才。

4

# Python 3，97字节

f=lambda s,p='	':min([s][:p[::-1]in p+p]+(s and[f(s[1:],p+s[0]),f(s[:-1],s[-1]+p)]or[p]),key=len)

2

c æ+0

2

# 重击，108字节

for((j=0;;j++)){
for((i=0;i<\${#1};i++)){
r=\${1:0:i}\${1:j+i}
[[ \$r = `rev<<<\$r` ]]&&echo "\${1:i:j}"&&exit
}
}

2

# Prolog，271位元组

p([_]).
p([X,X]).
p([X|Y]):-append([P,[X]],Y),p(P).

s(P,M,S,R,N):-p(P),append([M,S],N).
s(P,M,S,S,N):-p(S),append([P,M],N).
s(P,M,S,P,M):-append([P,S],X),p(X).

d(Y,P,N):-
findall([A,B,C],(append([R,M,X],Y),s(R,M,X,B,C),length(B,A)),S),
sort(1,@>,S,[[_,P,N]|_]).

palindrome([_]).
palindrome([X, X]).
palindrome([X | Xs]) :-
append([Prefix, [X]], Xs),
palindrome(Prefix).

palindrome_split(Prefix, Mid, Suffix, Prefix, N) :-
palindrome(Prefix),
append([Mid, Suffix], N).
palindrome_split(Prefix, Mid, Suffix, Suffix, N) :-
palindrome(Suffix),
append([Prefix, Mid], N).
palindrome_split(Prefix, Mid, Suffix, P, Mid) :-
append([Prefix, Suffix], P),
palindrome(P).

findall(
[La, Pa, Na],
(append([Prefix, Mid, Suffix], NP),
palindrome_split(Prefix, Mid, Suffix, Pa, Na),
length(Pa, La)),
Palindromes),
sort(1, @>, Palindromes, [[_, P, N] | _]).

2

## C ++，254个248 246字节

-6个字节归功于Zacharý-2个字节归功于Toby Speight

#include<string>
#define S size()
#define T return
using s=std::string;int p(s t){for(int i=0;i<t.S;++i)if(t[i]!=t[t.S-i-1])T 0;T 1;}s d(s e){if(!p(e))for(int i,w=1;w<e.S;++w)for(i=0;i<=e.S-w;++i){s t=e;t.erase(i,w);if(p(t))T e.substr(i,w);}T"";}

• 我用作T宏定义是因为R""对字符串文字（作为定义原始字符串文字的前缀，请参阅cppreference以获取更多信息）的另一影响是在执行此操作时不存在T""
• 预处理程序定义不能在同一行上，并且名称和内容之间至少要有一个空格
• 2个功能：p(std::string)测试字符串是否是回文。如果是，则返回1强制转换为true，否则返回0，强制转换为false
• 该算法会在每次擦除1个元素时循环回传整个字符串，以测试是否是回文，然后测试从第一个索引到擦除2个元素（将其循环到字符串的最大大小）the last index - number of erased char。如果发现擦除某个部分是回文，则返回。例如，当将字符串"aabcdbaa"作为参数传递时，cd都是有效答案，但是此代码将返回，c因为在测试是否擦除d并测试回文之前先进行擦除并测试是否为回文
• 这是要测试的代码：

std::initializer_list<std::pair<std::string, std::string>> test{
{"800233008","2"},
{ "racecarFOOL","FOOL" },
{ "abcdedcba","" },
{ "ngryL Myrgn","L " },
{ "123456789","12345678" },
{ "aabcdbaa","c" },
{ "[[]]","[[" },
{ "a","" },
{ "aabaab","b" }
};

for (const auto& a : test) {
if (a.second != d(a.first)) {
std::cout << "Error on : " << a.first << " - Answer : " << a.second  << " - Current : " << d(a.first) << '\n';
}
}

Toby Speight

Toby Speight

1

# PHP 104 + 1字节

while(~(\$s=\$argn)[\$e+\$i++]?:++\$e|\$i=0)strrev(\$t=substr_replace(\$s,"",\$i,\$e))==\$t&&die(substr(\$s,\$i,\$e));

1

snd.minimum.([]#)
p#s@(a:b)=[(i,take i s)|i<-[0..length s],(==)<*>reverse\$p++drop i s]++(p++[a])#b
p#_=[]

1

# JavaScript，90个字节

a=>a.map((_,p)=>a.map((_,q)=>k||(t=(b=[...a]).splice(q,p),k=''+b==b.reverse()&&t)),k=0)&&k

1

# JavaScript（ES6），91 78字节

(s,i=0,j=0,S=[...s],b=S.splice(i,j))=>S+''==S.reverse()?b:f(s,s[++i]?i:!++j,j)

1

# TSQL（2016）349B

DECLARE @i VARCHAR(255)='racecarFOOL'
;WITH DAT(v,i,l)AS(SELECT value,(ROW_NUMBER()OVER(ORDER BY value))-1,LEN(@i)FROM STRING_SPLIT(REPLICATE(@i+';',LEN(@i)+1),';')WHERE value<>'')
SELECT TOP 1C,S
FROM(SELECT LEFT(D.v, D.i)+SUBSTRING(D.v,D.i+E.i+1,D.l)C,SUBSTRING(D.v,D.i+1,E.i)S
FROM DAT D CROSS APPLY DAT E)C
WHERE C=REVERSE(C)
ORDER BY LEN(C)DESC

MickyT

1

# 外壳，18个字节

◄LfmS=↔†!⁰ṠM-Qŀ⁰Q⁰

## 说明

◄LfmS=↔†!⁰ṠM-Qŀ⁰Q⁰  Input is a string, say s="aab"
ŀ⁰    Indices of s: x=[1,2,3]
Q      Slices: [[],[1],[1,2],[2],[1,2,3],[2,3],[3]]
ṠM-       Remove each from x: [[1,2,3],[2,3],[3],[1,3],[],[1],[1,2]]
†!⁰          Index into s: ["aab","ab","b","ab","","a","aa"]
mS=↔             Check which are palindromes: [0,0,1,0,1,1,1]
f             Q⁰  Filter the slices of s by this list: ["aa","aab","ab","b"]
◄L                  Minimum on length: "b"

1

""#0
(h#n)t|(==)=<<reverse\$h++drop n t=take n t|x:r<-t=(h++[x])#n\$r|m<-n+1=t#m\$h

1

1

# C ++，189个186 176 167字节

#include<string>
using s=std::string;s d(s e){for(int i,w=0;;++w){s t=e.substr(w);for(i=-1;++i<=t.size();t[i]=e[i])if(t==s{t.rbegin(),t.rend()})return e.substr(i,w);}}

## 说明

{
for (int w=0; ; ++w) {
std::string t = e.substr(w);
for (int i=0;  i<=t.size();  ++i) {
if (t == std::string{t.rbegin(),t.rend()})
// We made a palindrome by removing w chars beginning at i
return e.substr(i,w);
t[i] = e[i];  // next candidate
}
}
}

foobar
↓↓↓↓
obar
foobar
↓
fbar
foobar
↓
foar
foobar
↓
foor
foobar
↓
foob

## 测试程序

HatsuPointerKun的答案直接得到提升：

static const std::initializer_list<std::pair<std::string, std::string>> test{
{ "800233008", "2" },
{ "racecarFOOL", "FOOL" },
{ "abcdedcba", "" },
{ "ngryL Myrgn", "L " },
{ "123456789", "12345678" },
{ "aabcdbaa", "c" },
{ "[[]]", "[[" },
{ "a","" },
{ "aabaab", "b" }
};

#include <iostream>
int main()
{
for (const auto& a : test) {
if (a.second != d(a.first)) {
std::cout << "Error on: " << a.first
<< " - Expected: " << a.second
<< " - Actual: " << d(a.first) << '\n';
}
}
}

0

## REXX，132个字节

a=arg(1)
l=length(a)
do i=1 to l
do j=0 to l-i+1
b=delstr(a,i,j)
if b=reverse(b) & m>j then do
m=j
s=substr(a,i,j)
end
end
end
say s

0

# 红宝石，86 84字节

->s{l=i=0
(l+=(i+=1)/z=s.size-l+1
i%=z)while(w=s[0,i]+s[i+l..-1])!=w.reverse
s[i,l]}

• 由于Cyoce，节省了2个字节

Cyoce

@Cyoce谢谢。我很难记住所有运算符的优先级。
iamnotmaynard

0

# C（gcc），307字节

#define T malloc(K)
P(S,i,y,z,k,u,L,K,V)char*S;{char*M,*R,*E;K=strlen(S);M=T;R=T;E=T;for(i=0;i<K;++i){for(y=0;y<=K-i;++y){strcpy(M,S);for(z=y;z<y+i;E[z-y]=M[z],++z);for(k=y;k+i<=K;M[k]=M[k+i],++k);V=strlen(M);strcpy(R,M);for(u=0;u<V/2;L=R[u],R[u]=R[V-u-1],R[V-u-1]=L,++u);if(!strcmp(M,R))puts(E),exit(0);}}}