荒天帝


私信TA

用户名:ljhabc

访问量:3068

签 名:

等  级
排  名 425
经  验 4774
参赛次数 1
文章发表 126
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>

using namespace std;

int n,A,B,max_ans=-1;

string s,a,b;

int check(string str)    //用于检测字符串是否是正回文子串 

{

    if(str.length()%2==0)    //如果长度是奇数,直接判错

    {

        return 0;

    } 

    for(int i=0;i<str.length()/2;i++)    //如果不是回文,直接判错 

    {

        if(str[i]!=str[str.length()-1-i])

        return 0;

    }

    return 1;

int f_front(string ss)

{

    map<string,int>    mp;    //用于标记字符串是否出现过 

    int ans=0; 

    string temp;

    for(int i=0;i<ss.length();i++)    //枚举子串起点

    {

        for(int j=1;j<=ss.length();j++)    //枚举子串长度 

        {

            temp=ss.substr(i,j);

            if(check(temp)&&!mp.count(temp))    //如果这个串是正回文并且没有出现过

            {

                mp[temp]=1;

                ans++;

            } 

        }

    }

    return ans; 

}

int f_rear(string ss)

{

    map<string,int>    mp;    //用于标记字符串是否出现过 

    int ans=0; 

    string temp;

    for(int i=0;i<ss.length();i++)    //枚举子串起点

    {

        for(int j=1;j<=ss.length();j++)    //枚举子串长度 

        {

            temp=ss.substr(i,j);

            if(!check(temp)&&!mp.count(temp))    //如果这个串不是正回文并且没有出现过

            {

                mp[temp]=1;

                ans++;

            } 

        }

    }

    return ans; 

}

int main()

{

    cin >> n;

    cin >> s;

    for(int i=1;i<=s.length()-1;i++)    //枚举前串的长度

    {

        a=s.substr(0,i);    //前串,起点是0,长度是i 

        b=s.substr(i,s.length()-i);    //后串,起点是i,长度是length-i

        //cout << "前串:"+a << endl <<"后串:"+ b <<endl; 

        A=f_front(a);

        B=f_rear(b);

        if(A*B>max_ans)

        {

            max_ans=A*B;

        }

    } 

    cout << max_ans << endl;

    return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区