我个人觉得,这个问题的题目说的太垃圾了,十分不详细,很容易让人误解。
之前一直编译不通过,也未知其原因,在参考通过的程序之后,才get到该题目的意思,难道我语文不好?maybe。
根据程序,可以知道,该题目主要是判断两种情况的对称字符串的长度,一种是偶对称,如 ABCCBA,最大长度为6,字符串123321->51233214也属于此类,最大长度不是12,而是6。。。;另一种是奇对称,如 ABCDEDCBA,最大长度为9。或许是牛客网上的题目出现了乱码,不然怎么都解释不通,望大神指导。
#include <string>
#include <iostream>
using namespace std;
int main(){
string str;
int len;
int i,j,k,t;
while(getline(cin, str))
{
int maxlen = 0;
len = str.length();
for(i=0; i<len; i++)
{
if(str[i] == str[i+1]) // 偶对称
{
t = 2;
j = i - 1;
k = i + 2;
while(j>=0 && k<len && str[j]==str[k])
{
j--;
k++;
t += 2;
}
if(maxlen < t)
maxlen = t;
}
else if(i+2<len && str[i] == str[i+2])
{
t = 3;
j = i - 1;
k = i + 3;
while(j>=0 && k<len && str[j]==str[k])
{
j--;
k++;
t += 2;
}
if(maxlen < t)
maxlen = t;
}
}
cout << maxlen << endl;
}
return 0;
}