当前位置:网站首页>L3-2 Delete up to three characters (30 points)
L3-2 Delete up to three characters (30 points)
2022-08-09 23:13:00 【Here_SDUT】
Given a string consisting of all lowercase English letters, allowing you to delete up to 3 characters, how many different strings are possible as a result?
Input format: Enter a string consisting of all lowercase English letters in one line and the length is in the interval [4, 10^6].
Output format: Output the number of different strings in one line with at most 3 characters removed.
Sample input:
ababccSample output:
25Tip:
Delete 0 characters to get "ababcc".
Remove 1 character to get "babcc", "aabcc", "abbcc", "abacc" and "ababc".
Remove 2 characters to get "abcc", "bbcc", "bacc", "babc", "aacc", "aabc", "abbc", "abac" and "abab".
Delete 3 characters to get "abc", "bcc", "acc", "bbc", "bac", "bab", "aac", "aab", "abb" and "aba".
Analysis: Remember that dp[i][j] means consider the first i characters and delete the number of j programs, then forThere are two possibilities for the i-th character: delete or not delete, and the corresponding state transition equation is:
- Delete:
dp[i][j] += dp[i-1][j-1] - Do not delete:
dp[i][j] += dp[i-1][j]
However, the reality is not so good, so there may be repeated strings, such as: xxxxbdbxxxx (x represents any character), we found that deleting "bd" or "db" results in the same, that is, leaving a b,Here comes the repetition.
That is to say, if there is a string with the same letters before and after, and the length is k and k\leq j+1, then repetition will occur.Why j+1?Since only j characters can be deleted at most, if you want to delete this section of characters to only the head and tail, the length must be less than or equal to j+1 .So how many of these characters are repeated?
Since k-1 deletions have been consumed in order to delete only the head or tail of strings with the same head and tail, there are still j – k +1span> times can be deleted, and the corresponding number is dp[p-1][j-k+1], where p here refers to the subscript of the first letter of the string with the same ending.
Code:
#include #define LL long longusing namespace std;const int maxn = 1e6 + 10;const int inf = 0x3f3f3f3f;const double PI = acos(-1.0);typedef pair 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;//Initializationfor (int i = 1; i <= n; i++) {for (int j = 0; j <= 3; j++) {if (i < j) break;//Cannot delete, skip directlydp[i][j] += dp[i - 1][j];// do not delete iif (j) dp[i][j] += dp[i - 1][j - 1];//delete ifor (int k = i - 1; k >= 1 && j >= i - k; k--) {//Find i before whether there is the same as iif (s[i] == s[k]) {//Removedp[i][j] -= dp[k - 1][j - (i - k)];break;}}}}LL ans = 0;for (int i = 0; i <= 3; i++) ans += dp[n][i];//accumulatecout << ans;return 0;} 边栏推荐
- STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
- 2021(ICPC)亚洲区域赛昆明站(CGHIJLM)
- How to Make Your Company Content GDPR Compliant
- MySQL跨表、多表更新SQL语句总结
- 场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
- Use zeros(), ones(), fill() methods to generate data in TF
- Word怎么设置图片衬于文字下方?两种方法教你设置Word图片衬于文字下方
- DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
- Unity2D_背景粒子效果
- LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
猜你喜欢

Simple questions peek into mathematics

《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN

AI识万物:从0搭建和部署手语识别系统

Bean生命周期

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

Excel如何打出正负号?Excel打出正负号的方法

ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
![[Graphic and textual] How to reinstall Win7 system](/img/24/3acccb93e5e219f39477dc77229a58.png)
[Graphic and textual] How to reinstall Win7 system

Problems with compiling SIP with QGIS

APP自动化测试框架-UiAutomator2基础入门
随机推荐
2021(ICPC)亚洲区域赛昆明站(CGHIJLM)
4D Summary: 38 Knowledge Points of Distributed Systems
Optimization of SQL Statements and Indexes
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
万字总结:分布式系统的38个知识点
linux定时执行sql文件[通俗易懂]
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
SQL语句及索引的优化
蓝牙模块的分类和对应的属性特点
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
【泛型编程】模板全详解
Cookie, session, token
Bean生命周期
Deceptive Dice(期望计算)
supervisor 命令操作大全「建议收藏」
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
[corctf 2022] 部分
同步锁synchronized追本溯源
抽象类 or 接口
RHEL7系统修复rm -rf /boot /etc/fstab