自己写的,效果待查1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50int main(){
string s;
int c,maxlen = 0,templen = 0;
string maxstr;
cin>>s;
for (int i = 0; i < s.size(); ++i) { //奇数
c = 1,templen = 1;
if (maxlen < templen) {
maxlen = templen;
maxstr = s.substr(i,maxlen);
}
while(i-c>=0 && i+c<s.size()){
if(s[i-c] == s[i+c]) {
templen += 2;
if (maxlen < templen) {
maxlen = templen;
maxstr = s.substr(i-c,maxlen);
}
++c;
}else{
break;
}
}
}
for (int i = 0; i < s.size()-1; ++i) { //偶数
c = 1;
if(s[i] == s[i+1]) {
templen = 2;
if(maxlen<templen){
maxlen = templen;
maxstr = s.substr(i,maxlen);
}
while (i - c >= 0 && i + 1 + c < s.size()) {
if (s[i - c] == s[i + 1 + c]) {
templen += 2;
if (maxlen < templen){
maxlen = templen;
maxstr = s.substr(i-c,maxlen);
}
++c;
}else{
break;
}
}
}
}
cout<<maxlen<<endl;
cout<<maxstr<<endl;
return 0;
}