[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 ...
随机推荐
- SpringBoot+Mybatis-Plus+Mysql的保姆级搭建
本文通过简单的示例代码和说明,让读者能够了解Mybatis-Plus+Mysql的简单使用 必须说明的是,本文有部分内容是为了后续的微服务写的,所以如果只想用Mybatis-Plus的话,直接使用ba ...
- [ABC148F] Playing Tag on Tree
2023-03-04 题目 题目传送门 翻译 翻译 难度&重要性(1~10):5 题目来源 AtCoder 题目算法 最短路 解题思路 考虑到 T 想活得久, A 想尽早追上 T ,所以我们就 ...
- Java 多线程处理 for 循环数据
对于必须在for循环内进行查询的场景,可采用以下几种方式进行优化. 1.主线程与子线程无先后顺序 public static void main(String[] args) throws Inter ...
- mysql到底需不需要容器化?
前言:在容器化的时代,当然一切皆可容器化.在docker官网首页赫然有下面这几个大字.足以知道docker的优势.那么且问,mysql适合跑在docker中吗? 当然,这个问题有人说可以,也有人说不可 ...
- 数据可视化【原创】vue+arcgis+threejs 实现立体光圈闪烁效果
本文适合对vue,arcgis4.x,threejs,ES6较熟悉的人群食用. 效果图: 素材: 主要思路: 先用arcgis externalRenderers封装了一个ExternalRender ...
- 全局安装oh-my-zsh保姆教程
我的系统是CentOS 7.6,按流程走完后可以实现系统内所有用户都默认使用zsh且插件配置共享省去重复编写配置或软连接的烦恼 1 安装git yum -y install git 2 安装zsh y ...
- 当开源项目 Issue 遇到了 DevChat
目录 1. 概述 2. Bug 分析与复现 3. Bug 定位与修复 4. 代码测试 5. 文档更新 6. 提交 Commit 7. 总结 1. 概述 没错,又有人给 GoPool 项目提 issue ...
- 【网络爬虫笔记】爬虫Robots协议语法详解
Robots协议是指一个被称为Robots Exclusion Protocol的协议.该协议的主要功能是向网络蜘蛛.机器人等搜索引擎爬虫提供一个标准的访问控制机制,告诉它们哪些页面可以被抓取,哪些页 ...
- IP协议:连接你我,掌握互联网的关键
IP 基本认识 在之前的章节中,我们已经详细介绍了应用层和传输层的相关概念和原理,了解了进程之间如何进行可靠的数据传输.我们知道,传输层的头部包含了进程所使用的端口信息,这是为了确保数据能够正确地传递 ...
- BGP路由协议学习一
转载请注明出处: 1.BGP的特点: BGP使用TCP作为其传输层协议(端口号为179),使用触发式路由更新,而不是周期性路由更新. BGP能够承载大批量的路由信息,能够支撑大规模网络. BGP提供了 ...