题目: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. VC无窗口控制台程序

    VC无窗口控制台程序 #pragma comment(linker,"/subsystem:\"Windows\" /entry:\"mainCRTStartu ...

  2. Shell数值比较

    Shell数值比较 比较 描述 n1 -eq n2 检查n1是否与n2相等 n1 -ge n2 检查n1是否大于或等于n2 n1 -gt n2 检查n1是否大于n2 n1 -le n2 检查n1是否小 ...

  3. CodeForces - 1105D Kilani and the Game(多源BFS+暴力)

    题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...

  4. Python自动化测试框架——断言

    在自动化测试执行的过程中,我们往往希望可以自定生成报告,那如何再测试中进行验证呢?我们使用断言 import unittest class TestCount(unittest.TestCase): ...

  5. 大页(Huge Page)简单介绍

    x86(包括x86-32和x86-64)架构的CPU默认使用4KB大小的内存页面(getconf PAGESIZE),但是它们也支持较大的内存页,如x86-64系统就支持2MB大小的大页(huge p ...

  6. while循环处理列表和字典

    一.在列表之间移动元素 假设有一个列表,里面存放的是网站新注册但没有验证的用户,验证这些用户后,如何将它们移动到另一个已验证用户列表中呢? 其中一种方法是使用while循环,在验证用户的同时,将其从未 ...

  7. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  8. 生产环境下lnmp的权限说明

    https://www.cnblogs.com/zrp2013/p/4183546.html 有关权限说明:-rwxrw-r‐-1 root root 1213 Feb 2 09:39 50.html ...

  9. Postman调试依赖登录接口的3种方法

    在接口测试种, 我们经常会遇到有些接口登录后才能访问.我们在使用Postman调试这种接口时一般有3种方法: 依次请求 如果有登录接口的文档,或者通过抓包比较容易抓出登录请求的参数和格式,可以先使用P ...

  10. CF613A:Peter and Snow Blower

    用一个圆心在(x,y)的圆环覆盖一个n边形,顺或逆时针给出n边形所有顶点,求圆环最小面积. 卡了好久,各种傻逼错误.. 题目就是让我们固定一大一小两个边界圆,我们来看看这两个圆满足什么条件. 首先外面 ...