题目连接: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. Hadoop: the definitive guide 第三版 拾遗 第十章 之Pig

    概述: Pig的安装很简单,注意一下几点: 1.设置系统环境变量: export PIG_HOME=.../pig-x.y.z export PATH=$PATH:$PIG_HOME/bin 设置完成 ...

  2. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  3. jfinal集成spring cxf做webservice服务

    链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...

  4. oracle 11gR2默认密码修改

    很久以前装了Oracle,今天终于下决心要学一学了,结果一上午的时间就贡献给如何连接数据库上了 忘记了安装时设置的用户名和密码怎么办?查了下网上的资料,终于解决了! 方法一: 首先进入sqlplus: ...

  5. 一口一口吃掉Hibernate(八)——Hibernate中inverse的用法

    一.Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之 ...

  6. linux下的软件包安装

    linux下安装软件包有两种方法:源文件编译安装(source)和 rpm 安装. 1.源文件包安装的通用方法. 一般安装源代码的程序你得要看它的README,一般在它的目录下都有的. 01.配置: ...

  7. windows安装Apache,注册服务出现“(OS 5)拒绝访问。 : AH00369: Failed to open the WinNT service manager..."错误

    原文:http://blog.csdn.net/jaray/article/details/9950211 在安装Apache的时候,我下载的是zip格式,不是msi安装版,需要自己注册服务,才能在桌 ...

  8. Eclipse用法和技巧二十六:浅谈快捷键

    网络上到处都是eclipse有哪些常用的快捷键,其中还有很多讲得着实不错,这里就不再狗尾续貂而是谈谈别的这段时间的一些思考.最近加入了开发团队,代码量突突的上去了,同时也发现关于快捷键还是有很多细节, ...

  9. Shell脚本笔记

      如何查询文件里的某个字符串? grep “字符串” 文件 例:grep "abc" tmp.txt   如何将查询出来的内容赋给变量? str=$(grep "abc ...

  10. perl 当前包会覆盖父类的同名方法

    12.5.2 访问被覆盖的方法: 当一个类定义一个方法,那么该子过程覆盖任意基类中同名的方法. [root@wx03 test]# cat Horse.pm package Horse; our @I ...