首页 > 其他分享 >codeforces 159D D. Palindrome pairs( manacher+dp )

codeforces 159D D. Palindrome pairs( manacher+dp )

时间:2023-04-23 21:34:39浏览次数:49  
标签:159D pairs Palindrome MAX Len 字符串 include mx 回文


题目链接:

codeforces 159D


题目大意:

给出一个字符出,求取这个字符串中互相不覆盖的两个回文子串的对数。


题目分析:

  • 首先能够用manacher模板,因为这个算法处理的字符串的长度式奇数,所以我们首先将原字符串拓展,也就是用一个没有出现过的子串填充到每两个字符之间,首位也要添加,这样处理后得到的字符串一定是奇数长度,对于这个字符串,偶数位上的字符对应原字符串中i/2-1位置上的字符。
  • 然后就是利用manacher处理出的Len数组解决问题,Len记录的是以当前位置为中心的奇数长度的字符串的回文子串的长度的一半+1。
  • 然后我们首先处理出原字符串以每个位置结尾的回文串的个数,然后求取前缀和。
  • 然后枚举每个回文串,利用前面求取的前缀和就可以知道右端在当前回文串左端的左侧的回文串的个数。
  • 统计枚举的结果就是最终的答案。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 2007

using namespace std;

typedef long long LL;

int temp[MAX<<1];
int Len[MAX<<1];

int init ( char *st , int n )
{
    int i;
    temp[0] = -1;
    for ( int i = 1 ; i <= 2*n ; i+=2 )
    {
        temp[i] = -2;
        temp[i+1] = st[i/2];
    }
    temp[2*n+1] = -2;
    temp[2*n+2] = -3;
    temp[2*n+3] = 0;
    return 2*n+1;
}

void manacher ( int *st , int len )
{
    int mx = 0 , ans = 0 , po = 0;
    for ( int i = 1 ; i <= len ; i++ )
    {
        if ( mx > i )
            Len[i] = min ( mx - i , Len[2*po-i] );
        else 
            Len[i] = 1;
        while ( st[i-Len[i]] == st[i+Len[i]] )
            Len[i]++;
        if ( Len[i]+i > mx )
            mx = Len[i]+i , po = i;
    }
}

char s[MAX];
LL a[MAX<<1],ans;

int main ( )
{
    while ( ~scanf ( "%s" , s ) )
    {
        ans = 0;
        memset ( a , 0 , sizeof ( a ) );
        int n = strlen ( s );
        manacher ( temp , init ( s, n ) );
        for ( int i = 1 ; i <= 2*n ; i++ )
        {
            int x = Len[i];
            if ( i&1 ) 
                for ( int j = 1 ; j < x ; j += 2 ) 
                    a[i+j]++;
            else
                for ( int j = 0 ; j < x ; j += 2 )
                    a[i+j]++;
        }
        a[0] = 0;
        for ( int i = 1 ; i <= 2*n ; i++ )
            a[i] += a[i-1];
        for ( int i = 1 ; i <= 2*n ; i++ )
        {
            int x = Len[i];
            if ( i&1 )
                for ( int j = 1 ; j < x ; j += 2 )
                    ans += a[i-j-1];
            else 
                for ( int j = 0 ; j < x ; j += 2 )
                    ans += a[i-j-1];
        }
        printf ( "%lld\n" , ans );
    }
}


标签:159D,pairs,Palindrome,MAX,Len,字符串,include,mx,回文
From: https://blog.51cto.com/u_7936627/6218749

相关文章

  • Palindrome
    PalindromeTimeLimit:4000/2000ms(Java/Other)   MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):72   AcceptedSubmission(s):19ProblemDescriptionApalindromeisasymmetricalstring,thatis,astringreadidenticallyfromle......
  • Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)
    题目地址:传送门先用dp求出所有的符合要求的半回文串,标记出来。然后构造字典树。然后再dfs一遍求出所有节点的子树和,最后搜一遍就能找出第k个来了。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib......
  • LightOJ - 1044 Palindrome Partitioning(DP)
    题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分接着枚举j,如果[i,j]回文,那么dp[i]=min(dp[i],dp[j-1]+1)#include<cstdio>#include<cstring>#include<al......
  • Hi-C pairs 文件格式
    Hi-Cpairs文件格式##pairsformatv1.0#sorted:chr1-chr2-pos1-pos2#shape:uppertriangle#chromsize:chr1248956422#chromsize:chr2242193529#chromsize:chr3198295559#chromsize:chr4190214555#chromsize:chr5181538259#chromsize:chr6170805979#ch......
  • 洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
    #include<bits/stdc++.h>usingnamespacestd;inta,b;boolzs(intx){if(x%2>0){for(inti=3;i<x;i+=2)if(x%i==0)returnfalse;returntrue;}elsereturnfalse;}boolhws(intx......
  • 【APIO2014】Palindromes
    先说一下自己的SAM做法:看到回文串我们首先考虑对以下字符串建立SAM:正串+特殊字符1+特殊字符2+反串。这样也许能有一点用。晚上睡觉前我考虑的是对于正串的endpos在反串中......
  • CF245H Queries for Number of Palindromes
    对字符串s,多次询问,给你两个数L和R,问在字符串区间l到r的字串中,包含多少回文串。 #include<iostream>#include<queue>#include<cstring>#defineIOSstd::ios::syn......
  • [LeetCode] 2341. Maximum Number of Pairs in Array
    Youaregivena 0-indexed integerarray nums.Inoneoperation,youmaydothefollowing:Choose two integersin nums thatare equal.Removebothinte......
  • Count the Number of Fair Pairs
    CounttheNumberofFairPairsGivena0-indexed integerarray nums ofsize n andtwointegers lower and upper ,returnthenumberoffairpairs.Apa......
  • Palindrome Linked List
    SourceGivenasinglylinkedlistofcharacters,writeafunctionthatreturnstrueifthegivenlistispalindrome,elsefalse.题解1-使用辅助栈根据栈的......