Section 1.3 Calf Flac

另存一個記錄數組 date記錄字母 大小寫應統一 op記錄在原數組中出現的位置

這就將原題轉換爲 字母求迴文了

再枚舉中間的情況2種 ASA 和ASSA

這樣就變得容易很多了 記住數組要開大

{
ID: yaoyuan4
PROG: calfflac
LANG: PASCAL
}
Program calfflac;
const
  inf = 'calfflac.in'; outf = 'calfflac.out';
type
  calf = record
   date : char;
   op : longint;
  end;
var
  s : array[0..20000] of char;
  f : array[0..20000] of calf;
  l : longint;
  ans : array[0..2] of longint;
Function ip(t : char) : char;
  begin
   if t in ['a'..'z'] then
    exit(chr(ord(t) - 32)) else exit(t);
  end;
Procedure init;
  var
   i : longint;
  begin
   assign(input, inf); reset(input);
   i := 0; l := 0; fillchar(ans, sizeof(ans), 0);
   repeat
    inc(i);
    read(s[i]);
    if s[i] in ['a'..'z', 'A'..'Z'] then
     begin
      inc(l);
      f[l].date := ip(s[i]);
      f[l].op := i;
     end;
   until eof(input);
   close(input);
  end;
Procedure ok(a, b : longint);
  var
   t : longint;
  begin
   if f[a].date <> f[b].date then
    begin
     inc(a);
     dec(b);
    end;
   t := b - a + 1;
   if t > ans[0] then
    begin
     ans[1] := a;
     ans[2] := b;
     ans[0] := b - a + 1;
    end;
  end;
Procedure work;
  var
   i, j, k : longint;
   m : boolean;
  begin
   for i := 2 to l-1 do
    begin
     j := i;
     k := i;
     while (f[j].date = f[k].date) and (j > 0) and (k < l) do
      begin
       dec(j);
       inc(k);
      end;
     ok(j, k);
     j := i - 1;
     k := i;
     while (f[j].date = f[k].date) and (j > 0) and (k < l) do
      begin
       dec(j);
       inc(k);
      end;
     ok(j, k);
    end;
  end;
Procedure print;
  var
   i : longint;
  begin
   assign(output, outf); rewrite(output);
   writeln(ans[0]);
   for i := f[ans[1]].op to f[ans[2]].op do
    write(s[i]);
   writeln;
   close(output);
  end;
begin
  init;
  work;
  print;
end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章