uva 10069 Distinct Subsequences 【dp+大数】
题目:uva 10069 Distinct Subsequences
题意:给出一个子串 x 和母串 s 。求子串在母串中的不同序列的个数?
分析:定义dp【i】【j】:x 的前 i 个字母在 s 的前 j 个字母中的出现次数;
dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ;
if ( x[ i ] == s [ j ] )
dp[i][j]=add(dp[i-1][j-1],dp[i][j]);
注意点:1:出现次数很大,要用大数加法
2::注意初始化
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 10050;
string dp[105][N];
string sum(string s1,string s2)
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}
int main()
{
//freopen("Input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
string s,x;
cin>>s>>x;
string ss="1",xx="2";
ss+=s,xx+=x;
for(int i=0;i<xx.size();i++)
dp[i][0]="0";
for(int i=0;i<ss.size();i++)
dp[0][i]="1";
for(int i=1;i<xx.size();i++)
{
for(int j=1;j<ss.size();j++)
{
dp[i][j]=dp[i][j-1];
if(xx[i]==ss[j])
dp[i][j]=sum(dp[i-1][j-1],dp[i][j]);
//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
} }
cout<<dp[xx.size()-1][ss.size()-1]<<endl;
}
return 0;
}
uva 10069 Distinct Subsequences 【dp+大数】的更多相关文章
- UVa 10069 Distinct Subsequences(大数 DP)
题意 求母串中子串出现的次数(长度不超过1后面100个0 显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数 当a[i]==b[j]&am ...
- 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] ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- UVA 10328 - Coin Toss dp+大数
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 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 ...
- [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 ...
随机推荐
- 运行外部exe
bool CFileOperate::lauchCAD() { //启动ZWCAD CString OutputPath; STARTUPINFO si; //一些必备参数设置 memset(& ...
- python之range (范围)
例题: for i in range(10): print(i,end='') # 输出结果 0123456789 # s = range(1,10) # 面试大坑 python2 和 python3 ...
- RTP/RTCP协议详解
1.简介 目前,在IP网络中实现实时语音.视频通信和应用已经成为网络应用的一个主流技术和发展方向,本文详细介绍IP协议族中用于实时语音.视频数据传输的标准协议RTP( Real-time Transp ...
- loj6063 Shadow
题目描述 题解: 显然凸多面体投下来一定是个凸多边形. 对于$30$分,直接投到$x-y$平面上即可. 对于$100$分,考虑搞出平面的一般式方程$ax+by+cz+d=0$. 给出平面上三个点$A, ...
- [LUOGU] P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈
https://www.luogu.org/problemnew/show/P3611 二分答案+优先队列 二分O(logn) 判一次正确性O(nlogn) 总体O(nlognlogn) 为了让pri ...
- 【转载】WampServer图标显示红色后变成橙色怎么解决
WampServer就是Windows Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件. 工具/原料 WampServer 方法/步 ...
- InnoDB体系架构总结(一)
缓冲池: 是一块内存区域,通过内存的速度来弥补磁盘速度较慢对数据库性能的影响.在数据库中读取的页数据会存放到缓冲池中,下次再读取相同页的时候,会首先判断该页是否在缓冲池中.对于数据库中页的修改操 ...
- python基础知识11-文件操作
文件 装饰器,装饰函数或者类的方法. 1.文件的基本操作 打开文件: 注意绝对路径与相对路径. path = 'text.txt' path = r'/home/pyvip/py_case/text. ...
- MySQL数据类型与操作
内容提要: 建表完整语法规范(create table 表格(字段名1 类型 (宽度) 约束条件)) MySQL数据库数据类型(整型.浮点型.字符类型(char与varchar).日期类型.枚举与集合 ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...