Maximum common sub-sequence time limit: theMs | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
Let's not beat around the bush, title, all you need to do is write a program that draws the longest common subsequence.
Tip: The longest common subsequence is also known as the longest common substring (not requiring continuous), and the abbreviation is LCS (longest Common subsequence). It is defined as a sequence s, if it is a subsequence of two or more known sequences, and is the longest of all conforming to this condition sequence, then S is called the longest common subsequence of the known sequence.
-
-
Input
-
-
the first line gives an integer N (0<n<100) that represents the number of data groups to be measured
The next two lines of data are two sets of strings to be tested. Each string length is not greater than 1000.
-
-
Output
-
-
each set of test data outputs an integer representing the longest common subsequence length. One row for each group of results.
-
-
Sample input
-
-
2asdfadfsd123abcabc123abc
-
-
Sample output
-
-
36
-
-
Source
-
-
Classic
-
-
Uploaded by
Hzyqazasdf
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int dp[1002][1002]; int main () {char a[1002],s[1002];int lena,lens,i,j,test;scanf ("%d", &test), while (test--) {scanf ("%s%s", a,s); Lena =strlen (a); Lens=strlen (s), memset (Dp,0,sizeof (DP)), for (i=1;i<=lena;i++) {for (j=1;j<=lens;j++) {if (a[i-1]==s [J-1]) Dp[i][j]=dp[i-1][j-1]+1;else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]);}} printf ("%d\n", Dp[lena][lens]);} return 0;}
Longest common subsequence (Nanyang oj36) (longest common sub-sequence)