Number of LCS

Source: Internet
Author: User

Test instructions

Given two strings, A, B, the number of LCS for a, B.

Exercises

In the LCS at the same time calculate the scheme qaq, but stupid I will not, will not, will not ...

Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int mod = 1e9 + 7;const int N = 1e3 + 7;int dp[n][n], Sum[n][n];char ch1[n], Ch2[n];int main () {scanf ("%s%s",  CH1 + 1, CH2 + 1); int n = strlen (ch1 + 1); int m = strlen (CH2 + 1); for (int i = 0; I <= N; ++i) sum[i][0] = 1;for (int j = 0; J <= M; J + +) Sum[0][j] = 1;for (int i = 1; I <= n; ++i) {for (int j = 1; j <= m; ++j) {if (Dp[i][j] < dp[i-1][j]) {dp[i ][J] = dp[i-1][j];sum[i][j] = sum[i-1][j];} else if (dp[i][j] = = Dp[i-1][j]) {sum[i][j] = (Sum[i][j] + sum[i-1][j])% MoD;} if (Dp[i][j] < dp[i][j-1]) {dp[i][j] = dp[i][j-1];sum[i][j] = sum[i][j-1];} else if (dp[i][j] = = Dp[i][j-1]) {sum[i][j] = (Sum[i][j] + sum[i][j-1])% MoD;}  if (ch1[i] = = Ch2[j]) {if (Dp[i][j] < dp[i-1][j-1] + 1) {Dp[i][j] = Dp[i-1][j-1] + 1;sum[i][j] = sum[i-1][j- 1];} else if (dp[i][j] = = Dp[i-1][j-1] + 1) {Sum[i][j] = (Sum[i][j] + sum[i-1][J-1])% mod;}}} printf ("%d\n", Sum[n][m]); return 0;}

  

Summarize:

A basic DP, I unexpectedly will not qaq, I really for "Zhi several" feel catch urgent ...

Number of LCS

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.