Light oj 1110 - An Easy LCS(LCS)
来源:互联网 发布:我的世界java net con 编辑:程序博客网 时间:2024/04/26 07:22
LCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them. Since there can be many solutions, you have to print the one which is the lexicographically smallest. Lexicographical order means dictionary order. For example, '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 two lines will contain two strings of length 1 to 100. The strings contain lowercase English 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: :(
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-12typedef __int64 ll;using namespace std;#define N 105int dp[N][N];char f[N][N][N];char a[N],b[N];int lena,lenb;void DP(){ int i,j; memset(dp,0,sizeof(dp)); for(i=1;i<=lena;i++) for(j=1;j<=lenb;j++) { if(a[i]==b[j]) { dp[i][j]=dp[i-1][j-1]+1; int k; strcpy(f[i][j],f[i-1][j-1]); f[i][j][dp[i-1][j-1]]=a[i]; } else if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i-1][j]; strcpy(f[i][j],f[i-1][j]); } else if(dp[i-1][j]<dp[i][j-1]) { dp[i][j]=dp[i][j-1]; strcpy(f[i][j],f[i][j-1]); } else if(dp[i-1][j]==dp[i][j-1]) { dp[i][j]=dp[i-1][j]; if(strcmp(f[i-1][j],f[i][j-1])<0) strcpy(f[i][j],f[i-1][j]); else strcpy(f[i][j],f[i][j-1]); } }}int main(){ int i,j,t,ca=0; scanf("%d",&t); while(t--) { scanf("%s%s",a+1,b+1); lena=strlen(a+1); lenb=strlen(b+1); memset(f,0,sizeof(f)); DP(); printf("Case %d: ",++ca); if(dp[lena][lenb]==0) printf(":(\n"); else printf("%s\n",f[lena][lenb]); } return 0;}
- Light oj 1110 - An Easy LCS(LCS)
- light oj An Easy LCS
- Light OJ 1110 - An Easy LCS (LCS+字典序最小路径记录)
- Light OJ 1110 An Easy LCS (DP+路径记录)
- lightoj 1110 - An Easy LCS (LCS输出路径)
- LightOJ - 1110 An Easy LCS(LCS)
- LightOj 1110 An Easy LCS
- LightOJ 1110 An Easy LCS
- light oj 1110 LCS 记录路径
- Light oj 1110 LCS带打印路径
- LightOJ 1110 An Easy LCS LCS路径输出
- LightOJ 1110 An Easy LCS--DP
- LightOJ 1110 An Easy LCS dp基础
- light oj 1159 - Batman LCS
- light oj 1013 LCS 应用
- Light oj 1157 - LCS Revisited(lcs 个数 记忆化搜索)
- lightoj 1110 - An Easy LCS 最长公共子序列+string
- Light oj 1159 - Batman(三维 LCS)
- [leetcode54] Spiral Matrix
- swift2.0 之 关键字
- 不使用 if、for 等逻辑关键字实现累加求和
- zoj 2417 Lowest Bit
- Android 百度地图(三):知识点小结
- Light oj 1110 - An Easy LCS(LCS)
- 字符串中的三个问题
- LeetCode(87) Gray Code
- 切换视图动画
- 项目24.2 油量监控
- 如何设置highcharts没有X和Y轴且X轴无刻度值
- Spider&&Weka
- 关于题目“DATAbase, ACM/ICPC NEERC 2009, UVa1592”的一个小问题探讨
- Linux使用笔记: 定制core dump文件的文件名