首页 > 其他分享 >[ABC299F] Square Subsequence

[ABC299F] Square Subsequence

时间:2023-04-29 09:11:39浏览次数:39  
标签:Sample Square string int nx Subsequence zz ABC299F dp

Problem Statement

You are given a string $S$ consisting of lowercase English letters. Print the number of non-empty strings $T$ that satisfy the following condition, modulo $998244353$.

The concatenation $TT$ of two copies of $T$ is a subsequence of $S$ (not necessarily contiguous).

Constraints

  • $S$ is a string consisting of lowercase English letters whose length is between $1$ and $100$, inclusive.

Input

The input is given from Standard Input in the following format:

$S$

Output

Print the answer.


Sample Input 1

ababbaba

Sample Output 1

8

The eight strings satisfying the condition are a, aa, ab, aba, b, ba, bab, and bb.


Sample Input 2

zzz

Sample Output 2

1

The only string satisfying the condition is z. Note that this string contributes to the answer just once, although there are three ways to extract the subsequence zz from $S = S_1S_2S_3 = $ zzz: $S_1S_2 = $ zz, $S_1S_3 = $ zz, and $S_2S_3 = $ zz.


Sample Input 3

ppppqqppqqqpqpqppqpqqqqpppqppq

Sample Output 3

580

考虑枚举两个串的开头,就设为 \(a\) 和 \(b\),满足 \(s_a=s_b\),然后分别跳子序列自动机。

为了使所有跳的操作不重复,我们应该强制第一个串的开头就是某个字母的第一次出现。

然后进行dp,定义 \(dp_{i,j}\) 为第一个串目前跳的到了点 \(i\),第二个串跳到了点 \(j\) 的情况。当然,要满足 \(i<a\)

理论上,要把所有的 \(dp_{i,j}\) 全部计入答案。但是我们发现如果这样会算重。比如串 ababab,那么枚举第一个 a时,会把 ab 这个串数两次。去一下重就可以了。如果 \(i\) 的下一个 \(s_a\) 的出现地方时 \(b\),那么我们才计入答案。

复杂度:枚举两个串开头是 \(O(n)\) 的,dp \(O(|\Sigma|n^2)\),总复杂度 \(O(n^3|\Sigma|)\)

#include<bits/stdc++.h>
using namespace std;
const int N=105,P=998244353;
int n,nx[N][26],dp[N][N],ans;
char s[N];
void add(int&x,int y)
{
	x=x+y>=P? x+y-P:x+y;
}
int main()
{
	scanf("%s",s+1),n=strlen(s+1);
	for(int i=n-1;i>=1;i--)
	{
		memcpy(nx[i],nx[i+1],sizeof(nx[i]));
		nx[i][s[i+1]-'a']=i+1;
	}
//	printf("%d\n",nx[1][0]);
	for(int i=0;i<26;i++)
	{
		int st=0;
		for(int j=1;j<=n;j++)
		{
			if(s[j]-'a'^i)
				continue;
			if(!st)
				st=j;
			else
			{
//				printf("hjh:%d %d\n",st,j);
				memset(dp,0,sizeof(dp));
				dp[st][j]=1;
				for(int a=st;a<j;a++)
				{
					for(int b=j;b<=n;b++)
					{
//						if(dp[a][b])
//							printf("%d %d\n",a,b);
						if(nx[a][i]==j)
							add(ans,dp[a][b]);
						for(int c=0;c<26;c++)
							if(nx[a][c]<j&&nx[a][c]&&nx[b][c])
								add(dp[nx[a][c]][nx[b][c]],dp[a][b]);
					}
				}
			}
		}
//		add(ans,(bool)st);
	}
	printf("%d",ans);
}

标签:Sample,Square,string,int,nx,Subsequence,zz,ABC299F,dp
From: https://www.cnblogs.com/mekoszc/p/17363559.html

相关文章

  • CF1814E Chain Chips & CF750E New Year and Old Subsequence - 动态 dp -
    一句话概括动态dp:用来解决带修改/多次区间询问的dp问题。将转移写成矩阵的形式,然后利用线段树求解区间问题/单点修改1814E注意一条边要么选2要么选0次,而且第一条边一定是选了2次。如果有一条边没选,那么这条边两侧的边一定都选了。设\(f_i\)代表考虑到第\(i\)条边,......
  • 「解题报告」AGC013E Placing Squares
    想了一会然后看题解,翻到日文题解然后又关了,然后突然会了,怎么回事第一眼生成函数!做不了。考虑经典拆贡献方法,把平方的贡献变成从区间中选两个数的方案数。这样我们可以用一个DP来计数。设\(f_{i,j}\)表示到了第\(i\)格,已经选了\(j\)个数的方案数。如果没有限制,那么就直......
  • Common Subsequence
    CommonSubsequenceTimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:43130 Accepted:17470DescriptionAsubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=<x1,x2,.........
  • CodeForces - 610B Vika and Squares (模拟)
    CodeForces-610BVikaandSquaresTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionVikahas n jarswithpaintsofdistinctcolors.Allthejarsarenumberedfrom 1 to n andthe......
  • Foursquare新功能:摇摇手机,签到轻松搞定
    有人可能会觉得之前Foursquare的签到功能有些麻烦,得慢慢掏出手机,搜出当前地点,点击“签到”才算完成。Foursquare推出的新功能就简单多啦,该公司利用NFC技术,用户只要摇一摇手机,就能自动对当前地点进行签到,这样用户也不那么容易忘记签到,也不会因为觉得麻烦而不对酒店咖啡厅的进行......
  • 维珍创始人理查德.布兰森投资Square
    继美国前财政部长,总统奥巴马的前任首席经济顾问、前哈佛大学校长劳伦斯·萨默斯六月加入移动支付公司Square的董事会后,Square又得到英国亿万富翁理查德.布兰森的青睐。看来这家移动支付新宠确实潜力巨大。据Square官方发言人KatieBaynes称,布兰森已向Square投资数百万美金,但拒绝透......
  • CF 1368B Codeforces Subsequences
    题目地址题意:给你一个数n,构造一个字符串,使得至少有n个子串为codeforcesSolution用贪心的思想肯定是只在codeforces基础上修改对于每个字符,对答案的贡献都是乘以字符的......
  • D2. Xor-Subsequence (hard version)
    D2.Xor-Subsequence(hardversion)/*进行转换,可以要存储i^a[i]的值首先确保前面都是相同的然后假设下一位是不相同的,那么会在这里进行更新,都枚举一下就可以了字......
  • Square(n) Sum
    InstructionsCompletethesquaresumfunctionsothatitsquareseachnumberpassedintoitandthensumstheresultstogether.Forexample,for[1,2,2]itsh......
  • 深度学习导论知识——最小二乘法(Ordinary Least Squares, OLS)
    1.知识点简介最小二乘法(OrdinaryLeastSquares,OLS)是常见的估计模型参数的方法。早在19世纪,勒让德就认为按照“误差的平方和最小”这个规则估计出来的模型是最接近......