Dp + high precision uva-10069-Distinct Subsequences

Source: Internet
Author: User

Returns the number of strings A and B in string. Solution: dp + high precision. Dp [I] [j] [k] indicates the number of the first I of string B in the first j of string A, and k indicates the number of digits, each digit represents a four-digit decimal number. If save2 [I] = save1 [j] Then dp [I] [j] = dp [I] [J-1] + dp [I-1] [J-1]; handle initialization issues. If save2 [I]! = Save1 [j] dp [I] [j] = dp [I] [J-1]; high precision: Each digit represents a four-digit decimal number, if the sum of one digit and one digit times out. Code: [cpp] # include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <queue> # define eps 1e-6 # define INF (1 <20) # define PI acos (-1.0) using namespace std; char save1 [11000], save2 [120]; int dp [120] [11000] [30]; // dp [I] [j] indicates the total number of one bits of Zi in Aj, which indicates 4-bit 10000 void add (int I, int j) {int i1 = I, j1 = J-1, i2 = I-1, j2 = J-1; for (int k = 1; k <= 30; k ++) {dp [I] [j] [k + 1] + = (dp [i1] [j1] [k] + dp [i2] [j2] [k]) /10000; // carry dp [I] [j] [k] + = (dp [i1] [j1] [k] + dp [i2] [j2] [k]) % 10000;} return;} int main () {int ca; scanf ("% d", & ca); while (ca --) {scanf ("% s", save1, save2); int len1 = strlen (save1), len2 = strlen (save2); memset (dp, 0, sizeof (dp); for (int j = 0; j <= len1; j ++) // pay attention to initialization, used to construct the first scenario dp [0] [j] [1] = 1; for (int I = 1; I <= len2; I ++) f Or (int j = 1; j <= len1; j ++) {if (save2 [I-1] = save1 [J-1]) {// dp [I] [j] = dp [I] [J-1] + dp [I-1] [J-1]; // You can inherit from dp [I] [J-1] or add dp [I-1] [J-1] add (I, j );} else {// dp [I] [j] = dp [I] [J-1]; for (int k = 1; k <= 30; k ++) // directly copy dp [I] [j] [k] = dp [I] [J-1] [k];} // printf ("% lld \ n ", dp [len2] [len1]); int I; for (I = 30; dp [len2] [len1] [I] = 0 & I> = 1; I --) // remove the front zero; printf ("% d", dp [len2] [len1] [I]); // process the first one without four digits. I --; for (; I> = 1; I --) printf ("% 04d", dp [len2] [len1] [I]); // note that the front zero putchar ('\ n');} return 0 ;}

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.