1110-an Easy LCS
|
PDF (中文版) |
Statistics |
Forum |
| Time Limit: 2 second (s) |
Memory Limit: MB |
LCS means ' longest Common subsequence ' that means the non-empty strings are given; You has to find the longest Common subsequence between them. Since there can be many solutions and you have to print the one which is the lexicographically smallest. lexicographical order means dictionary order. For example, the ' ABC ' comes before ' abd ' but ' Aaz ' comes before ' abc '.
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case starts with a blank line. The next and lines'll contain the length 1 to. The strings contain lowercase 中文版 characters only.
Output
For each case, print the case number and the lexicographically smallest LCS. If the LCS length is 0 then just print ':('.
| Sample Input |
Output for Sample Input |
3 Ab Ba Zxcvbn Hjgasbznxbzmx You Kjhs |
Case 1:a Case 2:ZXB Case 3::( |
Test instructions: Find the longest public self-sequence, with the same long dictionary order small. If the LCS length is 0, output the crying face.
Procedure: On the basis of the longest common subsequence, the dictionary order size of two strings is judged each time DP. The complexity of the longest common subsequence is 100*100, and the longest string is 100,o (10^6).
#include <stdio.h> #include <string.h> #include <string> #include <iostream>using namespace std ; #define MAX_N 110//again large DP array will explode O (n*m) char a[max_n],b[max_n];int dp[max_n+1][max_n+1];string dps[max_n][max_n];int mxxx (int a,int b) {return a>b?a:b;} int main () {int n,m,i,j,tem;int t;int cas=1;scanf ("%d", &t), while (t--)//a B-chain length {memset (dp,0,sizeof (DP));//DP in IJ Represents the maximum common subsequence length scanf ("%s", a), scanf ("%s", b), N=strlen (a), M=strlen (b), and for (i=0;i<=n;i++) for (int j=0;j<) of the front I and b chains of a chain. =m;j++) Dps[i][j].clear (); for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (A[i]==b[j])//dp The IJ from 1 calculates {if (dp[i+1][j]> DP[I][J+1]) Dps[i+1][j+1]=dps[i+1][j];else if (dp[i+1][j]<dp[i][j+1]) Dps[i+1][j+1]=dps[i][j+1];else if (dps[i+1] [j]<dps[i][j+1]) dps[i+1][j+1]=dps[i+1][j];elsedps[i+1][j+1]=dps[i][j+1];DP [I+1][j+1]=mxxx (dp[i+1][j],dp[i][ J+1]); if (dp[i+1][j+1]>dp[i][j]+1) Dps[i+1][j+1]=dps[i+1][j+1];else if (dp[i+1][j+1]<dp[i][j]+1) dps[i+1][j+1 ]=dps[i][j]+a[i];else if (Dps[i+1][j+1]>dps[i][j]+a[i]) dps[i+1][J+1]=DPS[I][J]+A[I];ELSEDPS[I+1][J+1]=DPS[I+1][J+1]; Dp[i+1][j+1]=mxxx (Dp[i][j]+1,mxxx (dp[i+1][j],dp[i][j+1));} Else{if (dp[i+1][j]>dp[i][j+1]) Dps[i+1][j+1]=dps[i+1][j];else if (dp[i+1][j]<dp[i][j+1]) dps[i+1][j+1]=dps[i ][j+1];else if (dps[i+1][j]<dps[i][j+1]) dps[i+1][j+1]=dps[i+1][j];elsedps[i+1][j+1]=dps[i][j+1];DP [i+1][j+1]= Mxxx (dp[i+1][j],dp[i][j+1]);}} printf ("Case%d:", cas++), if (Dp[n][m]) cout<<dps[n][m]<<endl;elseputs (":(");} return 0;}
Lightoj 1110-an Easy LCS Longest common sub-sequence +string