Ultraviolet A 10069 distinct subsequences (large number DP)

Source: Internet
Author: User
Zookeeper

The number of times the substring of the parent string appears (the length cannot exceed 1 and the last 100 zeros obviously need a large number)
If a is a sub-string B is the parent string d [I] [J], it indicates the number of times that the first I letter of the sub-string appears in the first J letters of the parent string when a [I] = B [J] & D [I-1] [J-1]! = 0 hour d [I] [J] = d [I-1] [J-1] + d [I] [J-1]
(A [I] = B [J] the number of times that the first I letter of the substring appears in the first J of the parent string is equal to the number of times that the first I-1 Letter of the Child string appears before the parent string the number of times the J-1 letters appear plus the number of times the first I letter of the substring appears in the first J-1 Letter of the parent string
A [I]! = B [J] the number of times that the first letter of the substring appears in the first J of the parent string is equal to the number of times that the first I letter of the substring appears in the first J-1 of the parent string times)
If you are too lazy to write a large number template, you can use Java to submit it;

import java.util.*;import java.math.*;public class Main {public static void main(String args[]) {BigInteger d[][] = new BigInteger[105][10005];Scanner in = new Scanner(System.in);int t = in.nextInt();while ((t--) != 0) {String b = in.next();String a = in.next();int la = a.length();int lb = b.length();for (int i = 0; i < la; ++i)for (int j = 0; j < lb; ++j)d[i][j] = BigInteger.ZERO;if (a.charAt(0) == b.charAt(0))d[0][0] = BigInteger.ONE;for (int j = 1; j < lb; ++j) {if (a.charAt(0) == b.charAt(j))d[0][j] = d[0][j - 1].add(BigInteger.ONE);elsed[0][j] = d[0][j - 1];}for (int i = 1; i < la; ++i)for (int j = 1; j < lb; ++j) {if (a.charAt(i) == b.charAt(j)&& d[i - 1][j - 1] != BigInteger.ZERO) {d[i][j] = d[i][j - 1].add(d[i - 1][j - 1]);} elsed[i][j] = d[i][j - 1];}System.out.println(d[la - 1][lb - 1]);}in.close();}}

C ++ code without a large number Template

#include<cstdio>#include<cstring>using namespace std;char b[10005], a[105];int d[105][10005], la, lb, t;void dp(){    memset(d, 0, sizeof(d));    for(int j = 1; j <= lb; ++j)    {        if(a[1] == b[j]) d[1][j] = d[1][j - 1] + 1;        else d[1][j] = d[1][j - 1];    }    for(int i = 2; i <= la; ++i)        for(int j = 1; j <= lb; ++j)        {            if(a[i] == b[j] && d[i - 1][j - 1])            {                d[i][j] = d[i][j - 1] + d[i - 1][j - 1];            }            else d[i][j] = d[i][j - 1];        }}int main(){    scanf("%s", &t);    while(t--)    {        scanf("%s%s", b + 1, a + 1);        la = strlen(a + 1);        lb = strlen(b + 1);        dp();        printf("%d\n", d[la][lb]);    }    return 0;}

Distinct subsequences

A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a sequenceX=X1X2...XM, Another sequenceZ=Z1Z2...ZKIs a subsequenceXIf there exists a strictly increasing sequence<I1,I2 ,...,Ik>Of indicesXSuch that for allJ= 1, 2 ,...,K, We haveXij=ZJ. For example,Z=BcdbIs a subsequenceX=AbcbdabWith corresponding index Sequence<2, 3, 5, 7>.

In this problem your job is to write a program that counts the number of occurrencesZInXAs a subsequence such that each has a distinct index sequence.

 

Input

The first line of the input contains an integerNIndicating the number of test cases to follow.

The first line of each test case contains a stringX, Composed entirely of lowercase alphabetic characters and having length no greater than 10,000. The second line contains another stringZHaving length no greater than 100 and also composed of only lowercase alphabetic characters. Be assured that neitherZNor any prefix or suffixZWill have more than 10100 distinct occurrences inXAs a subsequence.

 

Output

For each test case in the input output the number of distinct occurrencesZInXAs a subsequence. output for each input set must be on a separate line.

 

Sample Input

2
Babgbag
Bag
Rabbbit
Rabbit

 

Sample output

5
3



Ultraviolet A 10069 distinct subsequences (large number DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.