当前位置:网站首页>[Luogu p4590] garden party (DP set DP)
[Luogu p4590] garden party (DP set DP)
2022-07-28 03:58:00 【SSL_ TJH】
Garden party
Topic link :luogu P4590
The main idea of the topic
Give you a matching string , Then ask you for all the lengths n String , Satisfaction doesn't exist NOI The string of , Match the string LCS by x How many , For each x Find the answer .
( Both strings are composed of NOI Three characters make up )
Ideas
(LCS Is the length of the longest common subsequence , Tell it to people who don't know , For example, I )
DP set DP
There is one in the inner layer DP, Then the outer layer DP With inner layer DP The result comes as a state DP.
For example, a DP Something to find the answer , How many situations do you want to meet? The answer is this .
This question
First, let's think about the inner layer DP:
set up f i , j f_{i,j} fi,j Match the first string to i i i, The second string matches j j j Of LCS.
f i , j = f i − 1 , j − 1 + 1 ( s 1 i = s 2 j ) f_{i,j}=f_{i-1,j-1}+1(s1_i=s2_j) fi,j=fi−1,j−1+1(s1i=s2j)
f i , j = max { f i − 1 , j , f i , j − 1 } ( s 1 i ≠ s 2 j ) f_{i,j}=\max\{f_{i-1,j},f_{i,j-1}\}(s1_i\neq s2_j) fi,j=max{ fi−1,j,fi,j−1}(s1i=s2j)
Let's consider adding a new character to a string ( For example, the first string ), Then we have to maintain f i f_i fi This array .
( Since it is maintaining arrays , It must be more , Less maintenance )
But it still doesn't seem to work , Find some properties , It is found that the difference between adjacent two will not be greater than 1 1 1, So the difference is 2 ∣ s 2 ∣ 2^{|s2|} 2∣s2∣ Shape pressed .
Then you can preprocess the current array and put another character into which array it will go .
Then look at a limiting condition : Cannot appear NOI Substring .
Then this is not difficult , Record the current period N suffix , still NO suffix , Or nothing .
Then transfer it .
Code
#include<cstdio>
#include<iostream>
#define ll long long
#define mo 1000000007
using namespace std;
const int M = 1 << 15;
int n, K, nxt[M][3], fr[15], to[15];
ll f[2][M][3], ans[16];
char s[15], p[] = {
'N', 'O', 'I'};
int main() {
scanf("%d %d", &n, &K);
scanf("%s", s);
for (int i = 0; i < 1 << K; i++) {
for (int j = 0; j < K; j++) fr[j] = (i >> j) & 1;
for (int j = 1; j < K; j++) fr[j] += fr[j - 1];
for (int j = 0; j < 3; j++) {
for (int k = 0; k < K; k++) {
to[k] = (s[k] == p[j]) ? fr[k - 1] + 1 : max(fr[k], !k ? 0 : to[k - 1]);
nxt[i][j] |= (to[k] - (!k ? 0 : to[k - 1])) << k;
}
}
}
f[0][0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1 << K; j++) {
(f[(i & 1) ^ 1][nxt[j][1]][0] += f[i & 1][j][0] + f[i & 1][j][2]) %= mo;
(f[(i & 1) ^ 1][nxt[j][2]][0] += f[i & 1][j][0] + f[i & 1][j][1]) %= mo;
(f[(i & 1) ^ 1][nxt[j][0]][1] += f[i & 1][j][0] + f[i & 1][j][1] + f[i & 1][j][2]) %= mo;
(f[(i & 1) ^ 1][nxt[j][1]][2] += f[i & 1][j][1]) %= mo;
}
for (int j = 0; j < 1 << K; j++) for (int k = 0; k < 3; k++)
f[i & 1][j][k] = 0;
}
for (int i = 0; i < 1 << K; i++) {
int num = 0; for (int j = 0; j < K; j++) if ((i >> j) & 1) num++;
(ans[num] += f[n & 1][i][0] + f[n & 1][i][1] + f[n & 1][i][2]) %= mo;
}
for (int i = 0; i <= K; i++) printf("%lld\n", ans[i]);
return 0;
}
边栏推荐
- Read Plato farm's eplato and the reason for its high premium
- test case management tool
- ES6 from getting started to mastering 08: extended object functions
- Leetcode 0141. circular linked list - three solutions
- Simple and easy-to-use performance testing tools recommended
- xml文件使用及解析
- 过滤器、拦截器、监听器
- Interface automation test, complete introduction
- Common weak network testing tools
- ES6 from entry to mastery 07: Deconstruction assignment
猜你喜欢

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions

Leetcode skimming: dynamic programming 08 (segmentation and subsets)

LeetCode 0141. 环形链表 - 三种方法解决

简单、好用的性能测试工具推荐

Leetcode58. 最后一个单词的长度

Prefix-Tuning: Optimizing Continuous Prompts for Generation

《剑指offer》| 刷题小记

Super easy to use PC end long screenshot tool

CH340 RTS DTR引脚编程驱动OLED

Is there a bonus period for robot engineering
随机推荐
un7.27:redis数据库常用命令。
高等数学(第七版)同济大学 习题3-6 个人解答
Dynamic programming - 474. One and zero
Dynamic planning - 63. Different paths II
Dynamic programming - 416. Segmentation and subsets
Greed 45. Jumping game II
C语言力扣第45题之跳跃游戏 II。遍历跳跃
numeric_ Limits the range and related attributes of each data type learned
常用的接口测试工具
Lightpicture - exquisite drawing bed system
Linux - MySQL advanced (day19)
Data mining-02
基于SSM实现在线租房系统
Regression - linear regression
Redis cluster
Capacity expansion and reduction of RBD block storage device (VI)
Day08 redis的基础知识
数据丰富的计算:M.2在边缘遇到AI
[openvx] VX for basic use of objects_ convolution
Dynamic planning - 62. Different paths