Java糞?貫猟云嶄臥孀恷海議指猟忖憲堪
扮寂:2011-01-20 BlogJava lanxiazhi
1 * 佃業?兜雫
2 * 諒籾?貫補秘猟周calfflac.in嶄響函猟云?孀欺恷海議指猟堪?鍬廬岻朔才万徭失?吉議忖憲堪??峪深打忖銚?音曝蛍寄弌亟
3 * 補竃恷海指猟堪議海業?旺拝補竃万壓圻猟嶄議斤哘議堪。泌惚謹倖指猟堪海業?吉?補竃及匯倖。
4 * 廣?乎籾朕栖徭?http://ace.delos.com/usacogate?嗤佶箸議涛嗔辛參肇貧中廣過?載挫議膳楼峠岬。
5*/
6import java.util.*;
7import java.io.*;
8class calfflac
9{
10 public static void main (String [] args) throws IOException {
11 // Use BufferedReader rather than RandomAccessFile; it''s much faster
12 BufferedReader f = new BufferedReader(new FileReader("calfflac.in"));
13 // input file name goes above
14 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("calfflac.out")));
15 String temp=null;
16 StringBuilder origin=new StringBuilder(20000);//**根圻栖議忖憲堪
17 StringBuilder letters=new StringBuilder(20000);//**根忖銚
18 int[] indexes=new int[20000];
19 while((temp=f.readLine())!=null)
20 {
21 origin.append(temp);
22 origin.append(''\n'');
23 }
24 int len=origin.length();
25 for(int i=0;i<len;i++)
26 {
27 char c=(origin.charAt(i));
28 if(c>=''a''&&c<=''z''||c>=''A''&&c<=''Z'')//峪勣忖銚
29 {
30 letters.append(origin.charAt(i));
31 indexes[letters.length()-1]=i;
32 }
33 }
34 int maxLength=1;//指猟堪議海業
35 int maxIndex=0;//指猟堪議嶄寂忖銚議沫哈
36 len=letters.length();
37 for(int i=0;i<len;i++)//阿倖編
38 {
39 int length=maxLength+1;//孀和匯倖厚海議?咀葎籾朕勣箔?泌惚頁揖劔海業議?峪補竃恷念中議椎倖。
40 boolean isChanged=false;//指猟堪議海業辛參頁謎方倖?匆辛參頁甜方倖?宸倖喘噐俳算
41 for(;i-(length-1)/2>=0&&i+length/2<len;)
42 {
43 //宥狛輝念議i(指猟堪議嶄寂)?參式海業?孀欺棋霞協議匯粁忖憲堪旺霞編
44 if(ispal(letters,i-(length-1)/2,i+length/2))
45 {
46 maxLength=length;
47 maxIndex=i;
48 length+=2;
49 }
50 else if(!isChanged)//俳算
51 {
52 isChanged=true;
53 length++;//謎方倖才甜方倖俳算
54 }
55 else
56 break;
57 }
58 }
59 //朔中議旗鷹?繍孀竃指猟堪壓圻忖憲堪嶄議劔徨。
60 int start=indexes[maxIndex-(maxLength-1)/2];
61 int end=indexes[maxIndex+(maxLength)/2];
62 String result=origin.substring(start,end+1);
63 out.println(maxLength);
64 out.println(result);
65 out.flush();
66 out.close();
67 System.exit(0);
68 }
69 //登僅s嶄i欺j(脅淫根壓坪)岻寂議忖憲堪頁倦指猟。
70 static boolean ispal(StringBuilder s,int i,int j)
71 {
72 char c1=''0'',c2=''0'';
73 for(;i<j;i++,j--)
74 {
75 c1=s.charAt(i);
76 c2=s.charAt(j);
77 if(c1!=c2&&(c1-c2)%32!=0)
78 return false;
79 }
80 return true;
81 }
82}
83
|