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 ...
随机推荐
- 云服务器linux使用之开发环境搭建(一)
Host key verification failed. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: ...
- spring中bean的配置详解--定义parent
在工作中碰到了好多的配置文件,具体来说是spring 中bean配置的parent的配置,搞的我一头雾水,仔细看一下spring中有关bean的配置,剖析一下,具体什么含义! 一.Spring IoC ...
- Swift中Singleton的实现
一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...
- 题解 洛谷P3622/BZOJ1151【[APIO2007]动物园】
这一道题,我也是搞了很久才搞懂的(也就两个多小时). 感谢Rayment大佬的题解! 我们进入正题. 对于一个笼子里的动物,我们可以选择撤走或不撤走,可以用0和1来表示,很容易就想到二进制,想到状压d ...
- 【MySQL】索引和锁
前言 本文摘自数据库两大神器[索引和锁] 声明:如果没有说明具体的数据库和存储引擎,默认指的是MySQL中的InnoDB存储引擎 索引 在之前,我对索引有以下的认知: 索引可以加快数据库的检索速度 表 ...
- 第十三章:MFC库与Windows程序开发概述
主要内容: 1.Windows程序的基本结构 2.MFC库简介 3.使用Visual C++开发Windows程序 具体内容略
- 快速简单高效的搭建 SolrCloud 集群
转https://segmentfault.com/a/1190000008634902 集群配置 集群中的每台机器都要按照以下说明进行配置启动 首先到 solr 安装目录的 bin 下,编辑 sol ...
- jquery validate基本
http://www.runoob.com/jquery/jquery-plugin-validate.html jquery validate 默认 在键盘按下并释放及提交后验证提交表单 例如: $ ...
- ASP.NET MVC如何在页面加载完成后ajax异步刷新
背景:之前已写过两篇有关Ajax的随笔,这一篇是单独针对在页面加载完成的Ajax操作.比如说打开学生列表页面,先加载页面,然后以Ajax的方式,从数据库中检索相应的学生信息,给浏览者更好的体验. 简单 ...
- 如何设置目标并发(或者目标RPS)?
基本概念 首先您需要了解什么是并发用户.TPS 和它们之间的关系. 并发用户:指的是现实系统中同时操作业务的用户,在性能测试工具中一般称为虚拟用户(Virutal User).一般是站在客户侧评估的角 ...