好题… 就是比平时的 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. 如何为spring配置全局抛出异常注解

    0.首先了解一个注解@ControllerAdvice,他是spring里的一个注解,用于定义全局逻辑异常.数据绑定.请求处理等逻辑,与@ExceptionHandler等注解使用. Controll ...

  2. linux性能资源分析工具

    linux性能资源分析工具 1,top 2,ps 3,uptime 4,mpstat 5,pidstat 6,vmstat 7,iostat 8,netstat 9,lsof 10,sar / nmo ...

  3. 关于VS2022无法打开源文件<stdio.h>报错

    Q:本人今天下载VS2022,在billibilli观看到不靠谱教程以至于无法输出hello world A:经过网上查询发现 解决方案/侵删 web:https://www.cnblogs.com/ ...

  4. 【Vue】11 VueRouter Part1 概述 & 入门

    什么是路由? 即通过互联网把信息从源地址传输到目的地址的活动 路由决定数据包从来源到目的地的路径 转送将输入端的数据转移到合适的输出端 后端路由: 早起网站开发全部由服务器渲染,例如 Java的JSP ...

  5. 如何使用强化学习算法解决15-puzzle问题,即所谓的“十五谜题”推盘游戏

    相关: https://medium.com/@amshali/15-puzzle-with-reinforcement-learning-8bcfc1aa54e7 什么是15-puzzle问题? 数 ...

  6. .NET 屏幕录制

    窗口/屏幕截图适用于截图.批注等工具场景,时时获取窗口/屏幕图像数据流呢,下面讲下视频会议共享桌面.远程桌面这些场景是如何实现画面录制的. 常见的屏幕画面时时采集方案,主要有GDI.WGC.DXGI. ...

  7. 并查集基础 &打击罪犯

    并查集基础 真的很基础 题目描述:Description 某个地区有n(n<=1000)个犯罪团伙,当地警方按照他们的危险程度由高到低给他们编号为1-n,他们有些团伙之间有直接联系,但是任意两个 ...

  8. sqlserver 索引优化 CPU占用过高 执行分析 服务器检查[转]

    SELECT TOP 10 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans) ...

  9. 开发一个MutatingWebhook

    介绍 Webhook就是一种HTTP回调,用于在某种情况下执行某些动作,Webhook不是K8S独有的,很多场景下都可以进行Webhook,比如在提交完代码后调用一个Webhook自动构建docker ...

  10. Go make 介绍

    Go 语言中的 make 函数用于创建和初始化特定类型的对象,主要是用于创建切片(slice).映射(map)和通道(channel).make 函数与 new 函数不同,new 函数是用于分配内存, ...