好题… 就是比平时的 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. EdgeOne安全专项实践:上传文件漏洞攻击详解与防范措施

    前言 今天,我们将深入探讨上传文件漏洞攻击,这部分内容是EdgeOne专项实践篇的一部分.在本章中,我们不会涉及文件漏洞的含义.原理或站点配置等基础教程,如果你对这些内容感兴趣,可以参考这篇文章:探索 ...

  2. python os.path 模块详解

    python os.path 模块详解 os.path.basename() 返回最后一项,通常是文件名os.path.dirname() 返回的是目录,不包含文件名os.path.split() 返 ...

  3. 【Git】03 撤销 & 版本回退

    回退分为三种情况,每种情况对应了我们文件的存储区域 工作区 | 暂存区 | 版本区(当前分支) 1.文件可能存放在工作区,没有被Git追踪[红色标记状态] 2.文件可能已经添加到暂存区,没有被Git提 ...

  4. windows11系统NVIDIA显卡驱动自动升级导致2070 Super显卡失效 —— 552.22版本自动升级到560.70版本后2070 Super型号显卡停止工作

    操作系统 Windows11,旧版本显卡驱动是552.22,由于安装的是NVIDIA Geforce Experience后显卡驱动自动升级到560.77版本,然后显卡不再工作. 重新安装显卡驱动56 ...

  5. 同策略强化学习算法可以使用经验缓存池(experience buffer)吗 ??? 设计一个基于缓存池的改进reinforce算法,给出初步的尝试 ---------- (reinforce + experience buffer)

    本文使用代码地址: https://gitee.com/devilmaycry812839668/reinforce_with_-experience-buffer ================= ...

  6. 【转载】 【WarpDrive】GPU加速RL: 一块V100运行上千个智能体、数千个环境,这个「曲率引擎」框架实现RL百倍提速

    原文地址: https://mp.weixin.qq.com/s/Vw39k2PteEIGLRhmh2raLQ ============================================ ...

  7. [COCI2021-2022#6] Naboj 题解

    前言 题目链接:洛谷. 题意简述 给定一张无向图,每条边有个哨兵,初始在边的中间.你可以把某个结点旁边的哨兵全部吸引或远离这个结点.给出最后每个哨兵在边的哪一端,请构造出一种可能的操作方案或报告无解. ...

  8. quartz监控日志(四)自定义QuartzJobBean来实现监控

    quartz监控日志(一) quartz监控日志(二)添加监听器 quartz监控日志(三)查看卡死线程堆栈 上面几章介绍了quartz监控的几种方式,下面再介绍一种监听方式:自定义QuartzJob ...

  9. Viper:强大的Go配置解析库

    1 介绍 Viper是适用于Go应用程序的完整配置解决方案.它被设计用于在应用程序中工作,并且可以处理所有类型的配置需求和格式.目前Star 26.6k, 它支持以下特性: 设置默认值 从JSON.T ...

  10. ios滚动列表白屏问题

    移动端分页列表,在ios上滚动加载分页时候,使用scrollTop,会引起白屏问题. 看不少文章说是使用了-webkit-overflow-scrolling: touch;引起的硬件加速问题.亲测删 ...