1040. Longest Symmetric String (25)

来源:互联网 发布:人工智能语音聊天软件 编辑:程序博客网 时间:2024/06/05 09:57

考虑两种情况,ABA对称,AA对称,然后计算每个开始点的长度,取max
ps:本来以为会超时

#include<iostream>#include<string>using namespace std;int main(){    string str;    getline(cin,str);    int max=1;    for (int t = 1;t < str.size() ;t++)    {        int temp=1;        while (t - temp >= 0 && t + temp < str.size())//考虑ABA型        {            if (str[t - temp] != str[t + temp]) {break; }            else { temp++; }        }        temp--;        if (2 * temp + 1 > max) max = 2 * temp + 1;        temp = 1;        while (t - temp >= 0 && t + temp-1 < str.size())//考虑AA型        {            if (str[t - temp] != str[t + temp-1]) { break; }            else { temp++; }        }        temp--;        if (2 * temp > max) max = 2 * temp;    }    cout << max << endl;}
0 0