Problem C. String Game (dp)

Problem C. String Game

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes

Clair and Bob play a game. Clair writes a string of lowercase characters, in which Bob sets the puzzle
by selecting one of his favorite subsequence as the key word of the game. But Bob was so stupid that he
might get some letters wrong.
Now Clair wants to know whether Bob’s string is a subsequence of Clair’s string and how many times
does Bob’s string appear as a subsequence in Clair’s string. As the answer may be very large, you should
output the answer modulo 109 + 7.
Input
The first line is Clair’s string (whose length is no more than 5000), and the second line is Bob’s string
(whose length is no more than 1000).
Output
Output one line, including a single integer representing how many times Bob’s string appears as a
subsequence in Clair’s string. The answer should modulo 109 + 7.
Examples
standard input standard output
eeettt
et
eeettt
te
9
0

Question:
Give you a string of main strings and a string of substrings, allowing you to match substrings in the main string. The matched substrings do not need to be consecutive substrings, as long as they are in the same order as the substring characters, it will be counted as a successful match.
Problem solution:
dp, just write the transfer equation recursively

AC code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define dedebug() puts("what the fuck!")
#define debug(a) cout<<#a<<"="<<a<<endl;
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
#define ll long long
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
const int mod = 1e9 + 7;
ll gcd(ll x, ll y) {
    
    
	return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
    
    
	return x * y / gcd(x, y);
}
ll extends_gcd(ll a, ll b, ll& x, ll& y) {
    
    
	if (b == 0) {
    
    
		x = 1;
		y = 0;
		return a;
	}
	ll gcdd = extends_gcd(b, a % b, x, y);
	ll temp = x;
	x = y;
	y = temp - (a / b) * y;
	return gcdd;
}
char str[5010];
char sub[1010];
int dp[1010][5010];
int main() {
    
    
	while (~scanf("%s%s", str + 1, sub + 1)) {
    
    
		mem(dp, 0);
		int lenstr = strlen(str + 1);
		int lensub = strlen(sub + 1);
		for (int i = 1; i <= lensub; ++i) {
    
    
			for (int j = 1; j <= lenstr; ++j) {
    
    
				if (sub[i] == str[j]) {
    
    
					if (i == 1) {
    
    
						dp[i][j] = (dp[i][j - 1] + 1) % mod;
					}
					else {
    
    
						dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % mod;
					}
				}
				else {
    
    
					dp[i][j] = dp[i][j - 1];
				}
			}
		}
		printf("%d\n", dp[lensub][lenstr]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40924271/article/details/110006249