P2679 Sub-string
Test Instructions Topic Description
There are two strings that contain only lowercase letters \ (a\) and \ (b\).
Now remove \ (k\) a non-empty substring from the string \ (A\ ), and then put the \ (k\) substring in its string \ (a\) In the order in which they appear, they are concatenated together to get a new string. How many kinds of schemes can make this new string equal to the strings \ (b\) ?
Note: The location of the substring removed is also considered a different scenario.
Input output Format input format:
The first line is three positive integers \ (n,m,k\), each representing the length of the string \ (a\) , the length of the string \ (b\) , and the \ (k\)mentioned in the problem description. Each of the two integers is separated by a space.
The second line contains a string of length \ (n\) that represents the string \ (a\).
The third line contains a string of length \ (m\) that represents the string \ (b\).
Output format:
An integer that represents the number of scenarios that are being asked.
Because the answer can be large, it is required to output the result of the answer to \ (1000000007\) modulo.
Input and Output Sample input example:
6 3 1aabaabaab
Sample output:
2
Input Sample:
6 3 2aabaabaab
Sample output:
7
Input Sample:
6 3 3aabaabaab
Sample output:
7
Description
for section \ (1\) Group data:\ (1 \leq n \leq 500,1 \leq m \leq 50,k=1\);
for section \ (2\) Group to section \ (3\) Group data:\ (1 \leq n \leq 500,1 \leq m \leq 50,k=2\);
for section \ (4\) Group to section \ (5\) Group data:\ (1 \leq n \leq 500,1 \leq m \leq 50,k=m\);
for section \ (1\) Group to section \ (7\) Group data:\ (1 \leq n \leq 500,1 \leq m \leq 50,1 \leq k \leq m\);
for section \ (1\) Group to section \ (9\) Group data:\ (1 \leq n \leq 1000,1 \leq m \leq 100,1 \leq k \leq m\);
For all \ (10\) Group data:\ (1 \leq n \leq 1000,1 \leq m \leq 200,1 \leq k \leq m\).
Ideas
You can read a good blog. --alecli
This god Ben called for my problem.
Design state \ (dp[i][j][k][0/1]\),\ (i\) represents the \ (a\) string's front \ (i\) bit,\ (j\) means \ (b\) the front \ (j\) bit of the string,\ (k\) indicates how many substrings are selected,\ (0/1\) indicates that the current character is not selected in the substring.
If the bit is not selected, then the transfer is obviously easy to see:
\[dp[i][j][k][0]=dp[i-1][j-1][k][1]+dp[i-1][j-1][k][0]\]
It means that regardless of the previous selection or not, I add a space to separate the previous substring and the next substring.
If you want to choose this one, you should sort out whether the a\ and \ (b\) are the same.
- If different, then \ (dp[i][j][k][0]=0\);
- If the same, then \ (dp[i][j][k][0]=dp[i-1][j-1][k][1]+dp[i-1][j-1][k-1][1]+dp[i-1][j-1][k-1][0]\), which represents the continuation of the next substring, Restarts a new substring, directly at the beginning of a new substring, in the case of a continuous substring.
So the answer is \ (dp[n][m][k][0]+dp[n][m][k][1]\) .
By the way, my code is afraid of lack of space and wrote a scrolling array. If you do not write, remember to initialize the value of the \ (dp\) array.
AC Code
#include<bits/stdc++.h>using namespace std;typedef long long LL;const LL P=1000000007;LL n,m,k,dp[2][202][202][2];string a,b;int main(){ cin>>n>>m>>k; cin>>a>>b; a=' '+a; b=' '+b; dp[0][0][0][0]=dp[1][0][0][0]=1; for(LL i=1;i<=n;i++) for(LL j=1;j<=m;j++) for(LL p=1;p<=k;p++) { dp[i&1][j][p][0]=(dp[(i-1)&1][j][p][0]+dp[(i-1)&1][j][p][1])%P; if(a[i]==b[j]) dp[i&1][j][p][1]=(dp[(i-1)&1][j-1][p][1]+dp[(i-1)&1][j-1][p-1][0]+dp[(i-1)&1][j-1][p-1][1])%P; else dp[i&1][j][p][1]=0; } printf("%lld",(dp[n&1][m][k][1]+dp[n&1][m][k][0])%P); return 0;}
Luogu P2679 substring (string +dp)