好题… 就是比平时的 hard 难了一些……

虽然猜出是数位DP了…不过比我之前做的题,好像多了一维,印象中都是一维记录之前状态就够了……然后就没做出……

至于 KMP 的应用更是神奇,虽然掌握的 kmp 但是真的想不到……

窝的代码能力太差了……总归是学到了……希望下次能做出来吧……

参考题解 https://leetcode-cn.com/problems/find-all-good-strings/solution/shu-wei-dp-kmp-by-qodjf/

国服现在好多大神…题解写的真的很棒~

cal 计算小于等于任意字符串 s 的数目,计算两次求差就可了。其中 s1 没有被计算,所以单独算一次即可。

dp[i][j][0]  前i个字符 和evil有j个相同 并且已经小于 S 证明后面的字符可以任意选择

dp[i][j][1]  前i个字符 和evil有j个相同 并且等于 S 的前 i 个字符 所以后面的字符必须小于等于 S

已知当前有 j 个字符和 evil 的前 j 个字符相等,下一个字符是 c ,则增加下一个字符之后有几个字符相等,这个就根据next数组的含义很容易计算。理解kmp应该都能懂。

以上。

class Solution {
public:
static const int mod = 1000000007;
int findGoodStrings(int n, string s1, string s2, string evil) {
int m = evil.size();
int nt[m + 1];
nt[0] = -1;
for (int j = 0, k = -1; j < m; ) {
if (k == -1 || evil[j] == evil[k]) nt[++j] = ++k;
else k = nt[k];
}
int f = s1.find(evil) == string::npos;
return (cal(n, s2, evil, nt) - cal(n, s1, evil, nt) + f + mod) % mod;
}
int cal(int n, string &s, string &evil, int nt[]) {
int m = evil.size();
int dp[n + 1][m][2];
// dp[i][j][0] 前i个字符 和evil有j个相同 0不和s相等 1和s相等
memset(dp, 0, sizeof dp);
dp[0][0][1] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (char c = 'a'; c <= 'z'; c++) {
int len = getNextLen(evil, nt, j, c);
if (len < m) {
if (c < s[i]) {
up(dp[i+1][len][0], dp[i][j][1]);
}
if (c == s[i]) {
up(dp[i+1][len][1], dp[i][j][1]);
}
up(dp[i+1][len][0], dp[i][j][0]);
}
}
}
}
int ans = 0;
for (int i = 0; i < m; i++) {
up(ans, dp[n][i][0]);
up(ans, dp[n][i][1]);
}
return ans;
}
int getNextLen(string &evil, int nt[], int len, char c) {
while (len != -1 && evil[len] != c) len = nt[len];
return len + 1;
}
void up(int &x, int add) {
x = (x + add) % mod;
}
};

LeetCode 1397. Find All Good Strings 找到所有好字符串 (数位DP+KMP)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  3. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  4. LeetCode 389. Find the Difference (找到不同)

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  5. [LeetCode] 271. Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. LeetCode 277. Find the Celebrity (找到明星)$

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  8. LeetCode 162. Find Peak Element (找到峰值)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  9. [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  10. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

随机推荐

  1. springboot集成minIO

    文件系统:负责管理和存储文件的系统软件.操作系统通过文件系统提供的接口去存取文件,用户通过操作系统访问磁盘上的文件 minIO:轻量级服务分布式文件系统,适合存储非机构化数据.采用去中心化共享架构,结 ...

  2. 如何实现对ELK各组件的监控?试试Metricbeat

    一.前言 开发排查系统问题用得最多的手段就是查看系统日志,ELK 是 Elastic 公司开源的实时日志处理与分析解决方案,已经为日志处理方案的主流选择. 而在生产环境中,如何针对 ELK 进行监控, ...

  3. 【Layui】10 颜色选择器 ColorPicker

    文档地址: https://www.layui.com/demo/colorpicker.html 常规选择器: <fieldset class="layui-elem-field l ...

  4. 【GPU】如何两周内零经验手搓一个GPU | 美国工程师极限挑战 | 重写三次 | CUDA | SIMD | ISA指令集 | Verilog | OpenLane

    地址: https://www.youtube.com/watch?v=FTh-c2ek6PU

  5. 讲师招募 | Apache DolphinScheduler Meetup诚邀您共建开源!

    随着Apache DolphinScheduler在全球范围内的快速发展,我们的用户群体和社区活动也在不断扩大. 为了进一步丰富我们的社区内容,分享更多有价值的知识和经验,我们诚挚地邀请您加入我们,成 ...

  6. C语言的指定初始化

    ----------------版权声明:本文为CSDN博主「Supan-Yang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog ...

  7. ubuntu18.04 heirloom-mailx 通过外部SMTP服务器发送邮件

    配置软件源ubuntu18.04上无法直接安装heirloom-mailx,需要添加软件源 sudo vi /etc/apt/sources.list写入 deb http://cz.archive. ...

  8. 使用Ollama本地离线体验SimpleRAG(手把手教程)

    Ollama介绍 Ollama是一个开源项目,专注于开发和部署大语言模型,特别是像LLaMA这样的模型,用于生成高质量的文本和进行复杂的自然语言处理任务.Ollama的目标是让大语言模型的运行和使用变 ...

  9. How-many

    #include <bits/stdc++.h> #include <termio.h> #include <unistd.h> typedef long long ...

  10. 初三年后集训测试 T2--牛吃草

    初三年后集训测试 $T 2 $ 牛吃草 一言难尽 $$HZOI$$ $ Description $ 由于现代化进程的加快,农场的养殖业也趋向机械化. \(QZS\) 决定购置若干台自动喂草机来减少自己 ...