Uva10723-cyborg genes (LIS)

Source: Internet
Author: User

Question: uva10723-cyborg genes (LIS)


Give two strings to a new string, which maintains the character characteristics of the two strings, that is, the subsequences of the first two strings can be found in the string, evaluate the shortest length of such a string and the number of different types of strings.


Solution: Lis. First, we need to find the longest common subsequence, so that the length of the new string will be the minimum: L1 + L2-L [1] [N ];

L [I] [J]: The longest length of the first I character of the first string and the first J character of the second string.

N [I] [J]: makes the first I character of the first string and the first J character of the second string form the shortest Number of new strings.

S [I] = s [J], L [I] [J] = L [I-1] [J-1] + 1; N [I] [J] = N [I-1] [J-1];

S [I]! = S [J], L [I] [J] = max (L [I-1] [J], L [I] [J-1]); if (L [I-1] [J]> L [I] [J-1]) N [I] [J] = N [I-1] [J]; (because it is the type of the string to be shortest), and vice versa. N [I] [J] = N [I-1] [J] + N [I] [J-1];

Initialization: L [I] [0] = L [0] [J] = 0; n [0] [J] = N [I] [0] = 1;


Code:

#include <cstdio>#include <cstring>const int N = 35;typedef long long ll;int l[N][N];ll n[N][N];char s1[N], s2[N];int l1, l2;int Max (const int a, const int b) { return a > b? a: b; }void init () {l1 = strlen (s1);l2 = strlen (s2);memset (l, 0, sizeof (l));for (int i = 0; i <= l1; i++)n[i][0] = 1;for (int i = 0; i <= l2; i++)n[0][i] = 1;}int main () {int t;scanf ("%d%*c", &t);for (int k = 1; k <= t; k++) {printf ("Case #%d:", k);gets (s1);gets (s2);init ();for (int i = 1; i <= l1; i++) {for (int j = 1; j <= l2; j++) {if (s1[i - 1] == s2[j - 1]) {l[i][j] = l[i - 1][j - 1] + 1;n[i][j] = n[i - 1][j - 1];} else {l[i][j] = Max (l[i - 1][j], l[i][j - 1]); //printf ("%d %d %d %d %d\n", i, j, l[i - 1][j], l[i][j - 1], l[i][j]); if (l[i - 1][j] > l[i][j - 1])n[i][j] = n[i - 1][j];else if (l[i - 1][j] < l[i][j - 1])n[i][j] = n[i][j - 1];elsen[i][j] = n[i][j - 1] + n[i - 1][j];}}}printf (" %d %lld\n", l1 + l2 - l[l1][l2], n[l1][l2]);}return 0;}


Related Keywords:

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.