LeetCode 1397. Find All Good Strings 找到所有好字符串 (数位DP+KMP)
好题… 就是比平时的 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)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [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 ...
- [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 ...
- LeetCode 389. Find the Difference (找到不同)
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- 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 ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [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 ...
- LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description 给出一个未排序的数组,求出第一个丢失的正数. ...
随机推荐
- scratch少儿编程卡通三国背景72张全套素材包【免费下载】
scratch卡通三国题材背景图片,共72张,让你轻松打造scratch三国世界! 免费下载地址:https://www.xiaohujing.com.cn 这套背景图片以卡通风格呈现,色彩鲜艳.造型 ...
- 从C++看C#托管内存与非托管内存
进程的内存 一个exe文件,在没有运行时,其磁盘存储空间格式为函数代码段+全局变量段.加载为内存后,其进程内存模式增加为函数代码段+全局变量段+函数调用栈+堆区.我们重点讨论堆区. 进程内存 函数代码 ...
- 【Java】再谈Springboot 策略模式
第一次使用策略模式是一年前的一个项目: https://www.cnblogs.com/mindzone/p/16046538.html 当时还不知道Spring支持集合类型的自动装配 在最近一个项目 ...
- 【Mybatis】11 注解的使用
文档引用:http://www.mybatis.cn/archives/678.html 视频参考:https://www.bilibili.com/video/BV1NE411Q7Nx?p=15 注 ...
- 【Java】JDBC Part1 数据库连接的演变
环境搭建 使用Maven工程的依赖项,如果普通工程就点注释的地址下载jar包即可 <dependencies> <!-- https://mvnrepository.com/arti ...
- 【C】Re11 剩下的笔记
关于字符常量问题: #include <stdio.h> #include <stdlib.h> #include <string.h> void string01 ...
- B站上教虚幻引擎做游戏的博主 —— 谌嘉诚
个人主页地址: https://space.bilibili.com/31898841/ 课程地址: https://www.bilibili.com/video/BV164411Y732/
- NVIDIA Omniverse Audio2Face的简介
相关: https://www.zhihu.com/zvideo/1548363713740226561 建议参看: https://www.aiwht.com/sites/3406.html htt ...
- Redis中的Hash类型常用命令
一.hset命令作用:设置hash类型值:格式:hset key field value案例:192.168.0.111:0>hset product name 苹果"1" ...
- 从头搭建一个嵌入式web服务器-boa服务器
一.什么是boa? BOA是一款非常小巧的Web服务器,源代码开放.性能优秀.支持CGI通用网关接口技术,特别适合应用在嵌入式系统中. BOA服务器主要功能是在互联嵌入式设备之间进行信息交互,达到通过 ...