UVa 10069 Distinct Subsequences(大数 DP)
题意 求母串中子串出现的次数(长度不超过1后面100个0 显然要用大数了)
令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数 当a[i]==b[j]&&d[i-1][j-1]!=0时 d[i][j]=d[i-1][j-1]+d[i][j-1]
(a[i]==b[j]时 子串前i个字母在母串前j个字母中出现的次数 等于 子串前i-1个字母在母串前j-1个字母中出现的次数 加上 子串前i个字母在母串前j-1个字母中出现的次数
a[i]!=b[j]时 子串前i个字母在母串前j个字母中出现的次数 等于 子串前i个字母在母串前j-1个字母中出现的次数)
懒得写大数模版就用java交的 ;
import java.util.*;
import java.math.*; public class Main {
public static void main(String args[]) {
BigInteger d[][] = new BigInteger[105][10005];
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while ((t--) != 0) {
String b = in.next();
String a = in.next();
int la = a.length();
int lb = b.length(); for (int i = 0; i < la; ++i)
for (int j = 0; j < lb; ++j)
d[i][j] = BigInteger.ZERO; if (a.charAt(0) == b.charAt(0))
d[0][0] = BigInteger.ONE;
for (int j = 1; j < lb; ++j) {
if (a.charAt(0) == b.charAt(j))
d[0][j] = d[0][j - 1].add(BigInteger.ONE);
else
d[0][j] = d[0][j - 1];
} for (int i = 1; i < la; ++i)
for (int j = 1; j < lb; ++j) {
if (a.charAt(i) == b.charAt(j)
&& d[i - 1][j - 1] != BigInteger.ZERO) {
d[i][j] = d[i][j - 1].add(d[i - 1][j - 1]);
} else
d[i][j] = d[i][j - 1];
} System.out.println(d[la - 1][lb - 1]); }
in.close();
}
}
还有没加大数模版的C++代码
#include<cstdio>
#include<cstring>
using namespace std;
char b[10005], a[105];
int d[105][10005], la, lb, t;
void dp()
{
memset(d, 0, sizeof(d));
for(int j = 1; j <= lb; ++j)
{
if(a[1] == b[j]) d[1][j] = d[1][j - 1] + 1;
else d[1][j] = d[1][j - 1];
}
for(int i = 2; i <= la; ++i)
for(int j = 1; j <= lb; ++j)
{
if(a[i] == b[j] && d[i - 1][j - 1])
{
d[i][j] = d[i][j - 1] + d[i - 1][j - 1];
}
else d[i][j] = d[i][j - 1];
}
}
int main()
{
scanf("%s", &t);
while(t--)
{
scanf("%s%s", b + 1, a + 1);
la = strlen(a + 1);
lb = strlen(b + 1);
dp();
printf("%d\n", d[la][lb]); }
return 0;
}
Distinct Subsequences
A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a sequence X = x1x2…xm,
another sequence Z = z1z2…zk is a subsequence of X if there exists a strictly increasing sequence <i1,i2,
…, ik> of indices of X such that for all j = 1, 2, …, k, we have xij = zj. For example, Z = bcdb is
a subsequence of X =abcbdab with corresponding index sequence < 2, 3, 5, 7 >.
In this problem your job is to write a program that counts the number of occurrences of Z in X as a subsequence such that
each has a distinct index sequence.
Input
The first line of the input contains an integer N indicating the number of test cases to follow.
The first line of each test case contains a string X, composed entirely of lowercase alphabetic characters and having length no greater than 10,000.
The second line contains another string Z having length no greater than 100 and also composed of only lowercase alphabetic characters. Be assured that neither Z nor any prefix or suffix of Z will
have more than 10100 distinct occurrences in X as a subsequence.
Output
For each test case in the input output the number of distinct occurrences of Z in X as a subsequence. Output
for each input set must be on a separate line.
Sample Input
2
babgbag
bag
rabbbit
rabbit
Sample Output
5
3
UVa 10069 Distinct Subsequences(大数 DP)的更多相关文章
- uva 10069 Distinct Subsequences 【dp+大数】
题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- UVA 10069 Distinct Subsequences(DP)
考虑两个字符串,我们用dp[i][j]表示字串第到i个和字符串到第j个的总数,由于字串必须连续 因此dp[i][j]能够有dp[i][j-1]和dp[i-1][j-1]递推而来,而不能由dp[i-1] ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- js数字转换为float,取N个小数
javascript中的变量都是弱类型,所有的变量都声明为var,在类型转换过程中就没有java那么方便,它是通过 parseInt(变量).parseFloat(变量)等方法来进行类型转换的.注意: ...
- HTML---经常使用标签总结与实践
什么是HTML? 超文本标记语言,标准通用标记语言下的一个应用. "超文本"就是指页面内能够包括图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包含" ...
- Hadoop学习;測试;启动bug;secondary合并edits到fsimage
一个Hadoop集群拥有多个并行的计算机.用以存储和处理大规模的数据集 Hadoop强调代码向数据迁移 要执行的程序在规模上比数据小的多,更easy移动,此外通过网络移动数据比载入执行程序更花时间,这 ...
- Yocto tips (19): Yocto SDK Toolchian的使用
在使用之前须要先source env,导入各种环境变量(注意将路径变更成你自己的): source ../qt5_sdk/environment-setup-cortexa9hf-vfp-neon-p ...
- 对Java、C#转学swift的提醒:学习swift首先要突破心理障碍。
网上非常多都说swift是一门新手友好的语言. 但以我当年从Java转学Ruby的经验,swift对于从Java.C#转来的程序猿实际并不友好.原因就在于原来总有一种错觉:一个语言最重要的就是严谨,而 ...
- 安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)
TextView设置最多显示30个字符.超过部分显示...(省略号),有人说分别设置TextView的android:signature="true",而且设置android:el ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- localStorage、sessionStorage的区别
1.localStorage生命周期是永久的, sessionStorage生命周期为当前窗口或标签页,一旦窗口或标签页被永久关闭了,那么所有通过sessionStorage存储的数据也就被清空了. ...
- 005.JMS可靠性机制
1. 消息接收确认 JMS消息只有在被确认之后,才认为已经被成功地消费了.消息的成功消费通常包含三个阶段: 客户接收消息 客户处理消息 消息被确认 在事务性会话中,当一个事务被提交的时候,确认自动发生 ...
- 修复EJBInvokerServlet漏洞
1600/invoker/EJBInvokerServlet(存在命令执行) 修复方案: # 删除接口 # 设置中间件的访问控制权限,禁止web访问 /invoker 目录 http://www.cn ...