Uva10069-distinct subsequences (large number + dp)

Source: Internet
Author: User

Question: uva10069-distinct subsequences (large number + dp)


Returns the string a and B. Ask how many substrings B can be found in a, which can be discontinuous.


Solution: DP [I] [J] indicates the number of types of string B starting from the I-bit in string a starting from the J-bit.

B [I] = A [J] DP [I] [J] = DP [I-1] [J-1] + dp [I] [J-1 ];

B [I ]! = A [J] DP [I] [J] = DP [I] [J-1]; process the boundary. That is, only the last character in B matches.

10 ^ 100. Use a large number.


Code:

#include <cstdio>#include <cstring>#include <string>const int N = 10005;const int M = 105;const int base = 100000;char s1[N], s2[M];int max (const int a, const int b) { return a > b ? a: b; }struct bign {int len, s[30];bign () { memset (s, 0, sizeof (s));}bign (int num) { *this = num;}bign (const bign& b) { *this = b;}bign operator = (int num);bign operator + (const bign& b);bign operator + (const int b);bign operator += (const bign& b);void DelZore ();} dp[M][N];void bign::DelZore () {while (len >= 0 && s[len - 1] == 0) {len--;}if (len == 0)len = 1;}bign bign::operator = (int num) {if (num == 0) {len = 1;s[0] = 0;} else {len = 0;while (num > 0) {s[len++] = num % base;num = num / base;}}return * this;}bign bign::operator + (const bign& b) {bign c;c.len = 0;for (int i = 0, g = 0; g || i < max(len, b.len); i++) {int x = g;if (i < len) x += s[i];if (i < b.len) x += b.s[i];c.s[c.len++] = x % base;g = x / base;}return c;}bign bign::operator + (const int b) {bign b1;b1 = b;return *this + b1;}bign bign::operator += (const bign& b) {*this = *this + b;return *this;}int main () {int t, l1, l2;scanf ("%d%*c", &t);while (t--) {gets(s1);gets(s2);l1 = strlen (s1);l2 = strlen (s2);if (l2 == 0) {printf ("0\n");continue;}for (int i = 0; i < l2; i++) {dp[i][l1] = 0;}for (int i = l1 - 1; i >= 0; i--) {if (s1[i] == s2[l2 - 1]) dp[l2 - 1][i] = dp[l2 - 1][i + 1] + 1;elsedp[l2 - 1][i] = dp[l2 - 1][i + 1];}for (int i = l2 - 2; i >= 0; i--)for (int j = l1 - 1; j >= 0; j--) {dp[i][j] = dp[i][j + 1];if (s2[i] == s1[j])dp[i][j] += dp[i + 1][j + 1];}dp[0][0].DelZore();printf ("%d", dp[0][0].s[dp[0][0].len - 1]);for (int i = dp[0][0].len - 2; i >= 0; i--)printf ("%05d", dp[0][0].s[i]);printf ("\n");}return 0;}


 

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