首页 > 其他分享 >PTA 又见子串

PTA 又见子串

时间:2023-03-23 23:24:40浏览次数:29  
标签:子串 int PTA 字符串 include dp define

题目:

现在有两个字符串A和B,请你给出子串B在文本A中出现的次数(可以非连续出现)。即:设A={a1​...an​},B={b1​...bm​},请给出有多少组不同的I={i1​...im​}使得ai1​​=b1​,ai2​​=b2​...aim​​=bm​。不幸的是,本题的子串长度也可能很长。由于答案可能过大,请输出对10007取余后的结果。

输入格式:

两行,每行一个字符串。第一行为字符串A,第二行为字符串B。(len(B)<=len(A)<104)

输出格式:

一行,一个整数,为子串出现次数mod10007。

输入样例:

aabbccddeede
abcde
 

输出样例:

56

要求字符串B在A中出现了多少次,不要求连续(但要从左往右)
这是一道很简单的dp(听别人说的,但自己不会dp)
状态变量dp[i][j]表示 字符串A前i个字符包含字符串B前j个字符有多少个
状态转移方程:if(A[i]==B[j]) dp[i][j] = (dp[i-1][j] + dp[i-1][j-1])%10007;
else dp[i][j] = dp[i-1][j];
dp[][]方程初始化: dp[i][0] = 1; 表示A前i个字符中含B前0个字符1个;
代码:


点击查看代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<algorithm>
#include<fstream>
#include<iostream>
#include<cstdio>
#include<deque>
#include<string>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<unordered_map>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 310000
#define N 200010
#define endl '\n'
#define exp 1e-8
#define lc p << 1
#define rc p << 1|1
#define lowbit(x) ((x)&-(x))
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
inline ULL read() {
	ULL x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch>'9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void print(ULL x) {
	if (x > 9)print(x / 10);
	putchar(x % 10 ^ 48);
}
int dp[10100][10010];
int main()
{
	string a, b;
	cin >> a >> b;
	int n = a.size(), m = b.size();
	a = ' ' + a; b = ' ' + b;
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++)
	{
		dp[i][0] = 1;
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (a[i] == b[j]) dp[i][j] = (dp[i-1][j] + dp[i - 1][j - 1]) % 10007;
			else dp[i][j] = dp[i - 1][j];
		}
	}
	cout << dp[n][m] <<endl;
	return 0;
} 
/*也可以写滚动数组来优化空间
int dp[10100];
int main()
{
	string a, b;
	cin >> a >> b;
	int n = a.size(), m = b.size();
	a = ' ' + a; b = ' ' + b;
	dp[0] = 1;
	for (int i = 1; i <= n; i++)
	{
		for (int j = m; j >= 1; j--)
		{
			if (a[i] == b[j]) dp[j] = (dp[j - 1] + dp[j]) % 10007;
		}
	}
	cout << dp[m];
	return 0;
} 
*/

标签:子串,int,PTA,字符串,include,dp,define
From: https://www.cnblogs.com/wyh344866/p/17249875.html

相关文章

  • PTA 那就别担心了
    PTA那就别担心了给定一个有向无环图,给出起点\(st\)和终点\(ed\),问从起点出发的所有路径是否都能到达终点,并且让你求出从起点到终点的不同路径数量\(DFS\)记忆化搜索......
  • PTA 红豆生南国
    题目:有诗云:相思(王维唐)红豆生南国,春来发几枝。愿君多采撷,此物最相思。 那么,我们来采红豆吧!假设红豆树是这个样子的:这种红豆树的特点是:每个结点都有一......
  • 76.最小覆盖子串——学习笔记
    题目:给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串,则返回空字符串""。注意:对于t中重复字符,我们......
  • 华为OD机试 第 k 长子串
    本期题目:第k长子串......
  • LeetCode 3.无重复字符的最长子串
    题目链接在这里:​​3.无重复字符的最长子串-力扣(LeetCode)​​这道题学习了几何函数set()的用法1classSolution(object):2deflengthOfLongestSubstring(self,s:......
  • 密码学SAT入门文献1——Algebraic and Logic Solving Methods for Cryptanalysis
    密码学SAT入门文献2——CDCL(Crypto)SATSolversforCryptanalysis  Abstract Algebraicsolvingofpolynomialsystemsandsatisfiabilityofproposi......
  • 网络系统管理Linux环境——7.ROUTERSRV之IPTABLES
    题目要求服务器RouterSrv上的工作任务6. IPTABLES添加必要的网络地址转换规则,使外部客户端能够访问到内部服务器上的dns、mail、web和ftp服务。INPUT、OUTPUT和FOREARD链......
  • 华为OD 最长连续子串
    本期题目:最长连续子串......
  • Leetcode 5.最长回文子串(区间dp)
    题目链接在这里:5.最长回文子串-力扣(LeetCode)首先肯定是个n^2的算法,枚举起点也是必要的,但是枚举终点很显然不行,但是考虑到回文串会向下兼容,因此我们可以枚举长度,这就是......
  • pta python实验1-3
    7-1HelloWorld这是学习每种程序设计语言的第一个实例。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬输出Hell......