【CF】 159 D Palindrome pairs

点击打开链接

题意:给出一个字符串,求出字符串上一对不相交的回文子串 对数。(两个即一对)


O(n^2) 预处理字符串 得到左端开始回文个数记为DPL[ i ],右端开始回文 个数DPR[ i ]。

计算右端开始的前缀和。

sigma DPR[ i-1 ]*DPL[ i ];

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
typedef long long  LL;
const int MAXN = 714100;
const int MAXM = 140000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 109909
#define maxm 199909
int dl[2100],dr[2100];
int main()
{
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        for(int j=i,k=i;j>=0&&k<s.size()&&s[j]==s[k];j--,k++)
            dl[j]++,dr[k]++;
        for(int j=i,k=i+1;j>=0&&k<s.size()&&s[j]==s[k];j--,k++)
            dl[j]++,dr[k]++;
    }
    LL ans=0;
    for(int i=1;i<s.size();i++)
        dr[i]+=dr[i-1];
    for(int i=1;i<s.size();i++)
        ans+=dr[i-1]*dl[i];
    cout<<ans;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值