首页 > 其他分享 >题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]

题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]

时间:2024-07-14 14:07:52浏览次数:8  
标签:string Palindromic int characteristics ++ 题解 字符串 dp 回文

CodeForces 835D

D. Palindromic characteristics

time limit per test: 3 seconds
memory limit per test: 256 megabytes
*inputstandard input
outputstandard output

Palindromic characteristics of string \(s\) with length \(|s|\) is a sequence of \(|s|\) integers, where \(k\)-th number is the total number of non-empty substrings of \(s\) which are \(k\)-palindromes.

  1. A string is \(1\)-palindrome if and only if it reads the same backward as forward.
  2. A string is \(k\)-palindrome \((k > 1)\) if and only if:

Its left half equals to its right half.
Its left and right halfs are non-empty \((k - 1)\)-palindromes.
The left half of string \(t\) is its prefix of length \(⌊|t| / 2⌋\), and right half — the suffix of the same length. \(⌊|t| / 2⌋\) denotes the length of string \(t\) divided by \(2\), rounded down.

Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears \(3\) times.

Input

The first line contains the string \(s\) \((1 ≤ |s| ≤ 5000)\) consisting of lowercase English letters.

Output

Print \(|s|\) integers — palindromic characteristics of string \(s\).

Examples

input
abba
output
6 1 0 0 
input
abacaba
output
12 4 1 0 0 0 0 

Note

In the first example \(1\)-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is \(2\)-palindrome. There are no \(3\)- and \(4\)-palindromes here.

我的题解

一题比较简单的字符串dp

假设字符串长度为l
用一个二维数组记录两个字符之间的字符串是否为回文字符串(或者说组成了多少级别的回文字符串)
再用一个数组计数,该等级的回文字符串有多少个

先把自己组成回文字符串的挑出来(字符串长度为1)
再把和相邻的字符组成回文字符串的挑出来(字符串长度为2)

然后从3到n遍历字符串长度
检测是否存在该长度字符串为回文字符串
(只要看除了左右端点的也就是内部的字符串是否为回文字符串 和 左右两个字符是否相同 就可以了)
如果不是就continue
如果是就找这是多少级别的
只要查询最左边字符与中间字符之间的字符串构成多少级别的字符串再加一就可以了
找到最中间字符要对字符串长度分奇偶判断

That's all!


7.14更新: 预处理可以用马拉车Manacher 但是我还不会


我的代码:

#include <bits/stdc++.h>
#define int long long

const int N = 1e4/2 + 2;
int dp[N][N];
int k[N];
int n;

signed main()
{
	std::string s;
	std::cin >> s;
	n = s.size();
	
	for(int i = 0 ; i < n ; i ++)
	{
		dp[i][i] += 1;
		k[1] ++;
		if(i < n-1 && s[i] == s[i+1])
		{
			dp[i][i+1] += 2;
			k[1] ++;
			k[2] ++;
		}
	}
	
	for(int i = 3 ; i <= n ; i ++)
	{
		for(int j = 0 ; j+i-1 < n ; j ++)
		{
			int l = j , r = l+i-1;
			
			if(!dp[l+1][r-1] || s[l] != s[r]) continue;
			
			int mid = l + r >> 1;
			if(i&1) dp[l][r] = dp[j][mid-1] + 1;
			else dp[l][r] = dp[j][mid] + 1;
			
			for(int p = dp[l][r] ; p > 0 ; p --)
			{
				k[p]++;
			}
		}
	}
	
	for(int i = 1 ; i <= n ; i ++) std::cout << k[i] << " ";
	return 0;
}

PS:

思路来自大佬题解指路

标签:string,Palindromic,int,characteristics,++,题解,字符串,dp,回文
From: https://www.cnblogs.com/jiejiejiang2004/p/18301494

相关文章

  • 题解:CodeForces 1019 A Elections[贪心/三分]
    CodeForces1019AA.Electionstimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsyouknow,majorityofstudentsandteachersofSummerInformaticsSchoolliveinBerlandforthemostparto......
  • [CF1941E] Rudolf and k Bridges 的题解
    题目大意在第\((i,j)\)个格子修建一个桥墩需要\(a_{i,j}+1\)的花费而且要求\((i,0)\)与\((i,m)\)必须修建桥墩并且桥墩之间的距离不得大于\(d\)。现在需要求见\(k\)个连续的桥,求最小代价。其中\(1\lek\len\le100,3\lem\le2\cdot10,1\led\lem\)。思路因为......
  • [ABC206E] Divide Both 的题解
    题目大意求出从\(l\)至\(r\)中满足以下条件的\((x,y)\)的个数。\(\gcd(x,y)\ne1\)且\(\gcd(x,y)\nex\)且\(\gcd(x,y)\ney\)。其中\(1\lel\ler\le10^6\)。思路正难则反,所以可以求出所有互质或者是相互倍数的\((x,y)\)的对数,在将其减去所有的方案数就是......
  • [CF1178D] Prime Graph 的题解
    题目大意构造一个简单无向图,是所有的有度的点的度都是质数而且总共的边的数量的个数是质数。思路因为需要让所有的入度都为质数,所以我们可以找到两个相邻的质数\(2,3\),因为这样即使增加了一条边那么这个节点的度也是质数。先将这个图构成一个巨大的环,接着如果所有的边数并不......
  • [CF1538F] Interesting Function 的题解
    题目大意给定两个正整数\(l,r\),将\(l\)不断加\(1\)直到\(l=r\),求出这一过程中\(l\)发生变化的位数总数。\(1\lel<r\le10^9\)。思路假设从\(l\)处理到\(r\)变化的次数为\(f(l,r)\)。因为直接求解出\(f(l,r)\)十分困难,所以可以通过求出\(f(0,l)\)......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......
  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • [COCI2015-2016#6] PAROVI 的题解
    题意选择一些\(n\)一下互质的二元组\(\{a,b\}\),求对于任意\(x\in\big[2,n\big]\)都不满足\(a,b<x\)和\(a,b\gex\)的个数。简化题意因为无解的情况只发生在所有的\(\{a,b\}\)之间没有多余的位置用于放置\(x\),所以题意可以抽象成这样:选择一些区间互质的区间\([a......
  • [COCI2006-2007#4] ZBRKA 的题解
    题目大意在一个长度为\(n\)的排列中找出逆序对数量恰好为\(c\)的排列总数,其中\(1\len\le10^3,1\lec\le10^4\)。思路考虑将\(1\)到\(n\)这些数从小到大一次填进去,因为每一次填入的数多是最大的,所以逆序对增加的数量只与其所在的位置相关,所以设计\(f_{i,j}\)表......
  • Snow Walking Robot 的题解
    题目大意给你一个机器人和机器人的\(n\)个运动,要求你在给出的运动路径的基础上设计一种不会走重复的路径的方法,注意只能减少原来的步数而不能增加,其中\(1\len\le10^5\)。思路因为这道题目可以自由的配置路径并且要求机器人在最后回到原来的位置,那么就应该要到一种适合所有......