题目连接: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. Sereja and Bottles

    http://codeforces.com/problemset/problem/315/A 题目意思是第ai的瓶子能开bi的瓶子.给你相应的数据,求无法用其他瓶子打开的数量(即需要外力). 一开始看 ...

  2. SqlServer字段说明查询(表基本信息查询)

    --快速查看表结构(比较全面的) THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , col.name AS 列名 , ISNULL(ep.[v ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) ( 莫队 )

    莫队..先按sqrt(n)分块, 然后按块的顺序对询问排序, 同块就按右端点排序. 然后就按排序后的顺序暴力求解即可. 时间复杂度O(n1.5) --------------------------- ...

  4. Hibernate 多对多映射

    package com.entity.manytomany; import java.util.List; import javax.persistence.Entity; import javax. ...

  5. [转]-bash: wget: command not found的两种解决方法

    wget 时提示 -bash:wget command not found,很明显没有安装wget软件包.一般linux最小化安装时,wget不会默认被安装,这里是CentOS 6.5 64位系统 解 ...

  6. C-KMP

    一.BF算法 --传统算法 BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等,则比较S的 ...

  7. Linux解压乱码

    .向系统添加windows下的字符编码: sudo vim /var/lib/locales/supported.d/local 添加一下编码: zh_CN.GBK GBK zh_CN.GB2312 ...

  8. 基于visual Studio2013解决C语言竞赛题之0901文件读写

       题目

  9. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  10. 基于JSP+SERVLET的新闻发布系统(二)

    接下来讲解的是通过AJAX验证用户名是否已经添加 用户名: <input type="text" name="userName" id="use ...