当前位置:网站首页>L3-2 至多删三个字符 (30 分)
L3-2 至多删三个字符 (30 分)
2022-08-09 20:51:00 【Here_SDUT】
给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串?
输入格式: 输入在一行中给出全部由小写英文字母组成的、长度在区间 [4, 10^6] 内的字符串。
输出格式: 在一行中输出至多删掉其中 3 个字符后不同字符串的个数。
输入样例:
ababcc输出样例:
25提示:
删掉 0 个字符得到 “ababcc”。
删掉 1 个字符得到 “babcc”, “aabcc”, “abbcc”, “abacc” 和 “ababc”。
删掉 2 个字符得到 “abcc”, “bbcc”, “bacc”, “babc”, “aacc”, “aabc”, “abbc”, “abac” 和 “abab”。
删掉 3 个字符得到 “abc”, “bcc”, “acc”, “bbc”, “bac”, “bab”, “aac”, “aab”, “abb” 和 “aba”。
分析: 记 dp[i][j] 表示考虑前i个字符,删j个的方案数,那么对于第i个字符,有两种可能:删或者不删,对应的状态转移方程为:
- 删:
dp[i][j] += dp[i-1][j-1] - 不删:
dp[i][j] += dp[i-1][j]
然而现实没有那么美好,这样可能会出现重复的字符串,如:xxxxbdbxxxx(x表示任意字符),我们发现删去“bd”或者“db”的结果都相同,即剩下一个b,这里就产生的重复。
也就是说,如果有一段字符串前后字母相同,记长度为 k ,且 k\leq j+1,那么就会产生重复。为什么是j+1?因为最多只能删 j 个字符,所以要想将这一段字符删得只剩下头和尾,那么长度必须小于等于 j+1 。那么有多少个这样的字符重复了呢?
由于为了使得首尾相同的字符串删得只剩下头或者尾,已经消耗掉了 k-1次删除, 所以还剩下 j – k +1 次可以删去,对应的数量就是 dp[p-1][j-k+1],其中这里的p指的是这段收尾相同的字符串的首字母的下标。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
typedef pair<int, int> PII;
LL dp[maxn][5];
char s[maxn];
int main(int argc, char const *argv[]) {
cin >> s+1;
int n = strlen(s+1);
dp[0][0] = 1;//初始化
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 3; j++) {
if (i < j) break;//没法删,直接跳过
dp[i][j] += dp[i - 1][j];// 不删i
if (j) dp[i][j] += dp[i - 1][j - 1];//删i
for (int k = i - 1; k >= 1 && j >= i - k; k--) {//找i之前是否有和i相同的
if (s[i] == s[k]) {//去重
dp[i][j] -= dp[k - 1][j - (i - k)];
break;
}
}
}
}
LL ans = 0;
for (int i = 0; i <= 3; i++) ans += dp[n][i];//累加起来
cout << ans;
return 0;
}边栏推荐
- Photometric Stereo 光度立体法三维重建
- Definition and Basic Operations of Sequence Tables
- hdu 1503 Advanced Fruits(最长公共子序列的应用)
- Hessian Matrix 海森矩阵
- 面试官:Redis 大 key 要如何处理?
- PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
- Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
- visual studio 2022调试技巧介绍
- What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
- MySQL慢查询的多个原因
猜你喜欢

C语言之实现倒置字符串的两种方法

阿里二面:没有 accept,能建立 TCP 连接吗?

Lyapp exponents and bifurcation diagrams for fractional chaotic systems

微软word怎么转换成pdf文件?微软word转换为pdf格式的方法

Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week
【深度学习】pix2pix GAN理论及代码实现

How to fix Windows 11 not finding files

What to do if Windows 11 can't find Internet Explorer

浅谈Numpy中的shape、reshape函数的区别
[Deep learning] pix2pix GAN theory and code implementation
随机推荐
什么是IDE(集成开发环境)?
How to deal with keys when Redis is large?
RHEL7系统修复rm -rf /boot /etc/fstab
SecureCRT 设置超时自动断开连接时长
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
Word怎么制作一张标准的答题卡?
Word箭头上面怎么打字
matlab neural network ANN classification
CMake installation upgrade higher version
SQL语句及索引的优化
An overall security understanding and method of cyberspace based on connection and security entropy
Win11找不到Internet Explore怎么办
Access Characteristics of Constructor under Inheritance Relationship
Week 8 Deep learning for object detection
MySQL跨表、多表更新SQL语句总结
字节二面问的MySQL,差点没答好
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
Deceptive Dice(期望计算)
浅谈Numpy中的shape、reshape函数的区别
LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯