题目: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+大数】的更多相关文章

  1. UVa 10069 Distinct Subsequences(大数 DP)

     题意 求母串中子串出现的次数(长度不超过1后面100个0  显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数   当a[i]==b[j]&am ...

  2. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  3. 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] ...

  4. Distinct Subsequences (dp)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. UVA 10328 - Coin Toss dp+大数

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  6. 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 ...

  7. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  9. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 搜索 || DFS || POJ 2488 A Knight's Journey

    给一个矩形棋盘,每次走日字,问能否不重复的走完棋盘的每个点,并将路径按字典序输出 *解法:按字典序输出路径,因此方向向量的数组按字典序写顺序,dfs+回溯,注意flag退出递归的判断,并且用pre记录 ...

  2. 关于Error:Maven Resources Compiler: Maven project configuration required for module '项目名' isn't available. Compilation of Maven projects is supported only&

    总是出现Error:Maven Resources Compiler: Maven project configuration required for module '项目名' isn't avai ...

  3. 线程的start和run方法的区别

    回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...

  4. 5.1 qbxt 一测 T3

    反物质[问题描述] 物理学家有一种假设,世界上存在反物质,反物质遇到正常的物质会发生湮灭. 假设现在有 n 个粒子,每个粒子的种类用一个 m 以内的正整数表示.现在要将这些粒子按一定顺序放入一个封闭空 ...

  5. HDU-3746-Cyclic Nacklace(KMP,循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. Linux离线安装redis集群

    一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务 ...

  7. ES5的数组方法

    参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array.prot ...

  8. Sed命令基础操作

    sed用法的小技巧 (1)在查找范围时不需要用到替换,所以不用s; (2)当只需要打印被修改行时,可以使用-n 和 –p 选项,注意二者一定配合使用: 3种方式指定命令行上的多重指令 (1)用逗号分隔 ...

  9. 如何使用Visual Studio 2008(VS2008)编译C语言

    大家在学习C语言的时候接触的一般都是VC6.0.但是VC6.0只能编译C或者C++,不支持C#,集成度不是很高.而且界面并不十分友好,不能自动猜测关键字,函数的参数也不能自动标示.最关键的是,编译的时 ...

  10. 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...