[ABC299F] Square Subsequence
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);
}
[ABC299F] Square Subsequence的更多相关文章
- [Alg::DP] Square Subsequence
题目如下: #include <iostream> #include <string> #include <vector> using namespace std; ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
随机推荐
- [ABC146F] Sugoroku
2023-02-27 题目 题目传送门 翻译 翻译 难度&重要性(1~10):5 题目来源 AtCoder 题目算法 贪心 解题思路 对于第 ii 个点,只要到达 \(s_{i+1}\cdot ...
- PRACK消息
概述 PRACK消息是sip协议的扩展,在RFC3262中定义,标准的名称是sip协议中的可靠临时响应. 本文简单介绍标准中对PRACK消息流程的描述,以及fs配置PRACK的方式. 环境 cento ...
- 探索ChatGPT的Fine-tuning和Embeddings
1.概述 今天我们将深入探索ChatGPT的两项核心技术:Fine-tuning(微调)和Embeddings(嵌入).这些技术在现代自然语言处理领域扮演着至关重要的角色,为模型的性能提升和适应特定任 ...
- 《SQL与数据库基础》08. 多表查询
目录 多表查询 多表关系 一对多 多对多 一对一 多表查询概述 分类 内连接 外连接 自连接 联合查询 子查询 分类 标量子查询 列子查询 行子查询 表子查询 案例 本文以 MySQL 为例 多表查询 ...
- .NET周刊【8月第4期 2023-08-27】
国内文章 AgileConfig-1.7.0 发布,支持 SSO https://www.cnblogs.com/kklldog/p/agileconfig-170.html AgileConfig ...
- Python 导入 Excel多sheet
Python 导入 Excel多sheet 假设表格的样式如下 import os import sys import django BASE_DIR = os.path.dirname(os.pat ...
- QA||TypeError: ‘module‘ object is not callable报错怎么debugIHRM接口自动化测试
unittest.py生成测试报告时执行报错:TypeError: 'module' object is not callable 代码如下 原因:结合pycharm自动标注和报错信息,分析出应该是H ...
- RK3568开发笔记(六):开发板烧写ubuntu固件(支持mipi屏镜像+支持hdmi屏镜像)
前言 编译了uboot,kernel,buildroot后,可以单独输入固件,也可以整体打包成rootfs进行一次性输入,rootfs直接更新升级这个方式目前也是常用的. 烧写器软件:RKDe ...
- 【krpano】多分类缩略图及多分类地图案例
该案例提供了场景多分类缩略图展示以及多地图展示,效果如下截图: 下载地址:http://pan.baidu.com/s/1hsA5ta8 感谢群内小伙伴H·T·T的分享 ...
- js合并对象常用方法
const person = { name: 'David Walsh', gender: 'Male' }; const tools = { computer: 'Mac', editor: 'At ...