【牛客网】最长对称子串

给定一个字符串(数字或大小写字母), 找出最长的对称的子串(如有多个,输出任意一个)。
例如:
输入:“abbaad”
输出:“abba”
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

bool isReverse(string s)
{
    string t = s;
    reverse(t.begin(), t.end());    
    
    if (s == t)
        return true;
    else 
        return false;
}

int main()
{
    string s;
    string target, ans;
    
    cin >> s;
    
    for (unsigned i = 0; i < s.length(); i++)
    {
        for (int j = 0; j < i; j++)
        {
            target = s.substr(j, i);
            if (isReverse(target) && target.length() > ans.length())
            {
                ans = target;
            }
        }
    }
    
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/douzujun/p/10461863.html
今日推荐