usaco1.3.3 Calf Flac 我的题解
【问题描述】
据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'A'-'Z'和'a'-'z'。要你寻找的最长的回文的文章是一个不超过20,000个字符的字符串。我们将保证最长的回文不会超过2,000个字符(在除去标点符号、空格之前)。
PROGRAM NAME: calfflac
INPUT FORMAT: (file calfflac.in)
输入文件不会超过20,000字符。这个文件可能一行或多行,但是每行都不超过80个字符(不包括最后的换行符)。
OUTPUT FORMAT: (file calfflac.out)
输出的第一行应该包括找到的最长的回文的长度。
下一行或几行应该包括这个回文的原文(没有除去标点符号、空格),把这个回文输出到一行或多行(如果回文中包括换行符)。
如果有多个回文长度都等于最大值,输出最前面出现的那一个。
SAMPLE INPUT
Confucius say: Madam, I'm Adam.
SAMPLE OUTPUT
11
Madam, I'm Adam
【问题分析】
没想太多 直接dp,搜索超时 贴代码
有些过程用的直接是拼音 别吐槽啊 - -
1 { 2 ID:fuzhong2 3 PROG:calfflac 4 LANG:PASCAL 5 } 6 var 7 ch,cha:array[1..20000]of char; 8 pos,f:array[1..20000] of longint; 9 i,p,pp,max,maxp:longint; 10 11 function pd(c:char):boolean; //pd=判断 判断是否是字母 12 begin 13 if (97<=ord(c)) and (ord(c)<=122) then exit(true); 14 if (65<=ord(c)) and (ord(c)<=90) then exit(true); 15 exit(false); 16 end; 17 procedure print(p,max:longint); //打印 18 begin 19 while max<>0 do 20 begin 21 write(ch[p]); 22 inc(p); 23 if pd(ch[p-1]) then dec(max); 24 end; 25 end; 26 begin 27 assign(input,'calfflac.in');reset(input); 28 assign(output,'calfflac.out');rewrite(output); 29 p:=0;pp:=0; 30 while not eof(input) do 31 begin 32 inc(p); 33 read(ch[p]); 34 if pd(ch[p]) then begin inc(pp);pos[pp]:=p;cha[pp]:=lowercase(ch[p]); end; 35 end; 36 fillchar(f,sizeof(f),0); 37 for i:= 1 to pp do 38 f[i]:=i; 39 for i:=3 to pp do 40 begin 41 if cha[i]=cha[i-1] then f[i]:=i-1; 42 if cha[i]=cha[f[i-1]-1] then f[i]:=f[i-1]-1; 43 end; 44 max:=0; 45 maxp:=0; 46 for i:= 1 to pp do 47 begin 48 if i-f[i]>max then begin max:=i-f[i]+1; maxp:=f[i]; end; 49 end; 50 writeln(max); 51 print(pos[maxp],max); 52 writeln;// 这个地方不打writeln是不通过的 53 close(input); 54 close(output); 55 end.