题目连接:10069 - Distinct Subsequences

题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z。

解题思路:二维数组DP, 有类似于求解最长公共子序列, cnt[i][j]表示在x的前j个字符中有多少个z 前i个字符。

状态转移方程

1、x[j] != z[i]              cnt[i][j] = cnt[i][j - 1];

2、x[j] == z[i]   cnt[i][j] = cnt[i][j - 1] + cnt[i - 1][j - 1];

计算的时候使用高精度, 并且要见j == 0的情况归1, i == 0 的情况归0。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int N = 10005;
const int M = 105; struct bign {
int len, sex;
int s[M]; bign() {
this -> len = 1;
this -> sex = 0;
memset(s, 0, sizeof(s));
} bign operator = (const char *number) {
int begin = 0;
len = 0;
sex = 1;
if (number[begin] == '-') {
sex = -1;
begin++;
}
else if (number[begin] == '+')
begin++; for (int j = begin; number[j]; j++)
s[len++] = number[j] - '0';
} bign operator = (int number) {
char string[N];
sprintf(string, "%d", number);
*this = string;
return *this;
} bign (int number) {*this = number;}
bign (const char* number) {*this = number;} bign change(bign cur) {
bign now;
now = cur;
for (int i = 0; i < cur.len; i++)
now.s[i] = cur.s[cur.len - i - 1];
return now;
} void delZore() { // 删除前导0.
bign now = change(*this);
while (now.s[now.len - 1] == 0 && now.len > 1) {
now.len--;
}
*this = change(now);
} void put() { // 输出数值。
delZore();
if (sex < 0 && (len != 1 || s[0] != 0))
cout << "-";
for (int i = 0; i < len; i++)
cout << s[i];
} bign operator + (const bign &cur){
bign sum, a, b;
sum.len = 0;
a = a.change(*this);
b = b.change(cur); for (int i = 0, g = 0; g || i < a.len || i < b.len; i++){
int x = g;
if (i < a.len) x += a.s[i];
if (i < b.len) x += b.s[i];
sum.s[sum.len++] = x % 10;
g = x / 10;
}
return sum.change(sum);
}
}; bign cnt[M][N], sum;
char x[N], z[M]; int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%s%s", x, z);
int n = strlen(x), m = strlen(z);
for (int i = 0; i <= n; i++)
cnt[0][i] = 1; for (int i = 1; i <= m; i++) {
cnt[i][0] = 0;
for (int j = 1; j <= n; j++) {
cnt[i][j] = cnt[i][j - 1];
if (z[i - 1] == x[j - 1])
cnt[i][j] = cnt[i][j] + cnt[i - 1][j - 1];
}
}
cnt[m][n].put();
printf("\n");
}
return 0;
}

uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)的更多相关文章

  1. uva 10069 Distinct Subsequences 【dp+大数】

    题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...

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

  3. UVa 10069 Distinct Subsequences(大数 DP)

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

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

  5. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

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

  6. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

  7. UVa 103 - Stacking Boxes(dp求解)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  8. SPOJ 694 Distinct Substrings(不相同子串个数)

    https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...

  9. Distinct Subsequences (dp)

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

随机推荐

  1. 一步一步重写 CodeIgniter 框架 (9) —— 使用 CodeIgniter 类库

    通过前面几节的内容,我们从零开始搭建了一个非常方便的MVC框架,理解了 CodeIgniter 框架最核心的部分.然而一个框架的便利不仅仅在于提供一个MVC就可以了,它还必须具有较高的扩展性.下面将从 ...

  2. Unity3d 网络编程(二)(Unity3d内建网络各项參数介绍)

    这里是全部Unity3d在网络中能用到相关的类及方法.纵观參数功能, Unity3d来写一个手游是不二的选择: RPC 能够传递的參数 int float string NetworkPlayer N ...

  3. android的事件分发机制理解

    android的事件分发机制理解 1.事件触发主要涉及到哪些层面的哪些函数(个人理解的顺序,可能在某一层会一次回调其它函数) activity中的dispatchTouchEvent .layout中 ...

  4. Ibatis的分页机制的缺陷

    我们知道,Ibatis为我们提供了可以直接实现分页的方法 queryForList(String statementName, Object parameterObject, int skipResu ...

  5. struts+hibernate 请求数据库增删改查(小项目实例)

      StudentAction.java package com.action; import java.util.ArrayList; import java.util.List; import j ...

  6. 05-OC多态

    目录: 一.继承的缺陷 二.为什么使用继承 三.组合和聚合 四.多态 回到顶部 一.继承的缺陷 1 提高了程序的复杂度,维护性和扩展性低 2 破坏了类的封装性 回到顶部 二.为什么使用继承 1 代码复 ...

  7. ubuntu下配置qt+opengl+opencv

    原地址:http://www.cnblogs.com/aleny-liu/archive/2011/12/16/aleny-Qtnote1.html http://blog.csdn.net/jdh9 ...

  8. docker学习笔记4:利用docker hub上的mysql镜像创建mysql容器

    docker hub上有官方的mysql镜像,我们可以利用它来创建mysql容器,作为一个服务容器使用. 1.下载mysql镜像 docker pull mysql 2.创建镜像 docker run ...

  9. 基于visual Studio2013解决C语言竞赛题之0608水仙花函数

     题目 解决代码及点评 /* 功能:写一函数判断某数是否"水仙花数",所谓"水仙花数"是指一个三位数, 其各位数字立方和等于该数本身. */ #includ ...

  10. boost/lexical_cast.hpp的简单使用方法_行动_新浪博客

    boost/lexical_cast.hpp的简单使用方法_行动_新浪博客     boost/lexical_cast.hpp的简单使用方法    (2010-03-19 16:31:13)    ...