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的更多相关文章

  1. [Alg::DP] Square Subsequence

    题目如下: #include <iostream> #include <string> #include <vector> using namespace std; ...

  2. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  3. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  5. [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 ...

  6. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  7. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  8. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  10. [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 ...

随机推荐

  1. AI绘画:Stable Diffusion 终极炼丹宝典:从入门到精通

    本文收集于教程合集:AIGC从入门到精通教程汇总 我是小梦,以浅显易懂的方式,与大家分享那些实实在在可行之宝藏. 历经耗时数十个小时,总算将这份Stable Diffusion的使用教程整理妥当. 从 ...

  2. 搭建Minio分布式服务

    本文主要介绍Minio的分布式环境搭建,安装比较简单,因博主只有一台window,所以使用VM虚拟机搭建的. 搭建前可以先了解下minio: 1.官方文档:https://docs.min.io/cn ...

  3. TOML格式简介

    TOML(Tom's Obvious, Minimal Language)是一种用于配置文件的轻量级文本格式,旨在易于阅读和编写.它的设计目标是简单明了,同时也能表达复杂的数据结构.TOML文件通常用 ...

  4. Node练习 | 文件管理模块使用

    功能 新建一个Project文件夹, 里面是三个新建的文件, 分别是app.js/app.css/index.html 实现步骤 fs模块中的同步和非同步 同步 等待运行完成后再运行下一步 本次练习为 ...

  5. c语言代码练习3改进

    #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int x = 0; printf("请输入一 ...

  6. oj练习题程序编程题

    打印图形Description按要求输出由*组成的图案Input无需输入Output输出下面由"组成的图案卡 11 print('*') print("***") pri ...

  7. SQL 语句 增删改查、边学习边增加中..... 这一部分为select

    SQL语句按照最大的类别分为 1.增加 insert 2.删除 delete  https://www.cnblogs.com/kuangmeng/p/17756654.html 3.修改update ...

  8. Linux 中如何安全地抹去磁盘数据?

    哈喽大家好,我是咸鱼 离过职的小伙伴都知道,离职的时候需要上交公司电脑,但是电脑里面有许多我们的个人信息(聊天记录.浏览记录等等) 所以我们就需要先把这些信息都删除,确保无法恢复之后才上交 即有些情况 ...

  9. PoW、PoS、DPoS和PBFT简介

    1. 概览 PoW(工作量证明).PoS(权益证明).DPoS(委托权益证明)和PBFT(拜占庭容错)是区块链和分布式系统领域中常见的共识算法.下面将详细介绍这些共识算法的原理和特点: PoW(工作量 ...

  10. String类的valueOf以及动态绑定(多态)

    1.String类的valueOf Class String public static String valueOf(Object obj) Returns the string represent ...