pascal 回文数问题
发布网友
发布时间:2022-04-30 14:44
我来回答
共5个回答
热心网友
时间:2022-06-23 07:41
********************华丽的分割线*******************
var a,b:array[0..30000]of char;c:array[0..30000]of longint;
var q,w,max,i,j,n,x:longint;
begin
while not(eof) do
begin
inc(n);
read(a[n]);
end;
x:=0;
for i:=1 to n do
if a[i] in ['A'..'Z'] then begin x:=x+1;b[x]:=chr(ord(a[i])+32);c[x]:=i;end
else if a[i] in ['a'..'z'] then begin x:=x+1;b[x]:=a[i];c[x]:=i;end;
max:=0;
i:=1;
while i<x do
begin
j:=0 ;
while (b[i-j+1]=b[i+j])and(i-j+1>1)and(i+j<x) do
inc(j);
if b[i-j+1]<>b[i+j] then dec(j);
if j*2>max then begin max:=j*2;q:=c[i-j+1];w:=c[i+j];end;
j:=0;
while (b[i-j]=b[i+j])and(i-j>1)and(i+j<x) do
inc(j);
if b[i-j]<>b[i+j] then dec(j);
if j*2+1>max then begin max:=j*2+1;q:=c[i-j];w:=c[i+j];end;
inc(i);
end;
writeln(max);
if q=0 then q:=1;
if w=x+1 then w:=x;
for i:=q to w do
write(a[i]);
end.
********************华丽的分割线*******************
USACO里我的AC程序,绝对能过、
如果还有不懂,来找我
热心网友
时间:2022-06-23 07:41
program aa;
const
ph=['a'..'z','A'..'Z'];
var
a:array[1..20000]of char;
x1,x2,max,t1,t2,t3,t4,i,j,n:integer;
Function Right(t1,t2:integer;var t3,t4:integer):boolean;
var i,j:integer;
ch1,ch2:char;
begin
Right:=false;
i:=t1-1;
while (not (a[i] in ph)) and (i>0) do dec(i);
if i<=0 then exit;
j:=t2+1;
while (not (a[j] in ph)) and (j<=n) do inc(j);
if j>n then exit;
if ord(a[i])<97 then ch1:=a[i]
else ch1:=chr(ord(a[i])-32);
if ord(a[j])<97 then ch2:=a[j]
else ch2:=chr(ord(a[j])-32);
if ch1<>ch2 then exit;
right:=true;
t3:=i;t4:=j;
end;
begin
n:=0;
while not eoln do
begin
inc(n);
read(a[n]);
end;
max:=0;
for i:=1 to n do
if a[i] in ph then begin
j:=1;t1:=i;t2:=i;
while right(t1,t2,t3,t4) do
begin
inc(j);
t1:=t3;t2:=t4;
end;
dec(j);
if j*2+1>max then begin
max:=j*2+1;
x1:=t1;x2:=t2;
end;
end;
for i:=1 to n-1 do
if a[i] in ph then begin
j:=1;t1:=i+1;t2:=i;
while right(t1,t2,t3,t4) do
begin
inc(j);
t1:=t3;t2:=t4;
end;
dec(j);
if j*2>max then begin
max:=j*2;
x1:=t1;x2:=t2;
end;
end;
writeln(max);
for i:=x1 to x2 do write(a[i]);
writeln;
end.
热心网友
时间:2022-06-23 07:42
var
n,l,t,i:longint;
s:string;
begin
readln(n);
str(n,s);
l:=length(s);
t:=0;
for i:=1 to l div 2 do if s[i]s[l-i+1] then begin t:=1;break;end;
if t=0 then writeln('YES') else writeln('NO');
end.
热心网友
时间:2022-06-23 07:42
【USACO上的……】
const
ph=['a'..'z','A'..'Z'];
var
a:array[1..20000]of char;
x1,x2,max,t1,t2,t3,t4,i,j,n:integer;
Function Right(t1,t2:integer;var t3,t4:integer):boolean;
var i,j:integer;
ch1,ch2:char;
begin
Right:=false;
i:=t1-1;
while (not (a[i] in ph)) and (i>0) do dec(i);
if i<=0 then exit;
j:=t2+1;
while (not (a[j] in ph)) and (j<=n) do inc(j);
if j>n then exit;
if ord(a[i])<97 then ch1:=a[i]
else ch1:=chr(ord(a[i])-32);
if ord(a[j])<97 then ch2:=a[j]
else ch2:=chr(ord(a[j])-32);
if ch1<>ch2 then exit;
right:=true;
t3:=i;t4:=j;
end;
begin
assign(input,'calfflac.in'); reset(input);
assign(output,'calfflac.out'); rewrite(output);
n:=0;
while not eoln do
begin
inc(n);
read(a[n]);
end;
max:=0;
for i:=1 to n do
if a[i] in ph then begin
j:=1;t1:=i;t2:=i;
while right(t1,t2,t3,t4) do
begin
inc(j);
t1:=t3;t2:=t4;
end;
dec(j);
if j*2+1>max then begin
max:=j*2+1;
x1:=t1;x2:=t2;
end;
end;
for i:=1 to n-1 do
if a[i] in ph then begin
j:=1;t1:=i+1;t2:=i;
while right(t1,t2,t3,t4) do
begin
inc(j);
t1:=t3;t2:=t4;
end;
dec(j);
if j*2>max then begin
max:=j*2;
x1:=t1;x2:=t2;
end;
end;
writeln(max);
for i:=x1 to x2 do write(a[i]);
writeln;
close(output);
end.
应该懂了嘛??如果要测试数据,我这里还有…………
热心网友
时间:2022-06-23 07:43
usaco的吧......
枚举即可