这题有一个坑:非正回文子串,表示的是除了正回文子串以外的所有的子串。。。
题目地址:https://www.dotcpp.com/oj/problem1826.html
#include<bits/stdc++.h>
using namespace std;
///ios::sync_with_stdio(false);
#define ll long long
const int maxn=100010;
int re[maxn]; ///某个点切断时的正回文数量
int not_re[maxn]; ///某个点切断时的非正回文数量
set<string>u1,u2; ///集合去重以及统计子串数量
ll max(ll a,ll b)
{
if(a>b)
{
return a;
}
else return b;
}
bool is_huiwen(string str)///判断字符串是否是回文
{
int i=0;
int j=str.length()-1;
while(i<j)
{
if(str[i]==str[j])
{
i++;
j--;
}
else return false ;
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
int n;
string str;
cin>>n>>str;
for(int i=0;i<n;i++)
{
for(int j=i;j>=0;j-=2)
{
int len=i-j+1;
string tmp=str.substr(j,len);
if(is_huiwen(tmp)&&tmp.length()%2)
{
u1.insert(tmp);
}
}
re[i]=u1.size();
}
for(int i=n-1;i>0;i--)
{
for(int j=i;j<n;j++)
{
int len=j-i+1;
string tmp=str.substr(i,len);
if(tmp.length()%2&&is_huiwen(tmp))
{
continue;
}
u2.insert(tmp);
}
not_re[i]=u2.size();
}
ll ans=0;
for(int i=1;i<n;i++)
{
ans=max(ans,re[i-1]*not_re[i]);
}
cout<<ans<<endl;
return 0;
}