LightOJ 1110 - An Easy LCS(最长公共子序列 路径输出 三维 模板啊)
来源:互联网 发布:国家电网网络大学题库 编辑:程序博客网 时间:2024/04/28 21:31
题目链接:http://lightoj.com/volume_showproblem.php?problem=1110
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: :(
PS:
字典序输出!
转自:http://blog.sina.com.cn/s/blog_7fec19cd010112jm.html
代码如下:
/*最长公共子序列:dp[][]存的是到当前位置,最长序列的个数,s[][][]存的是到当前位置最长的公共子序列,其实dp[][],和s[][][]的原理是一样的,不同的是,s[][]存的是一个序列,所以是三维的*/#include <stdio.h>#include <string.h>#define N 150int dp[N][N];char s[N][N][N];char s1[N], s2[N];int main(){ int t,i,j,k; int cas = 0; scanf("%d",&t); while(t--) { scanf("%s%s",s1+1,s2+1); memset(s,0,sizeof(s)); for(i = 1; s1[i]; i++) for(j = 1; s2[j]; j++) { if(s1[i] == s2[j]) { dp[i][j] = dp[i-1][j-1]+1; for(k = 0; k < dp[i-1][j-1]; k++) s[i][j][k] = s[i-1][j-1][k]; s[i][j][k] = s1[i]; } else { if(dp[i-1][j] > dp[i][j-1]) { dp[i][j] = dp[i-1][j]; strcpy(s[i][j],s[i-1][j]); } else if(dp[i][j-1] > dp[i-1][j]) { dp[i][j] = dp[i][j-1]; strcpy(s[i][j],s[i][j-1]); } else { dp[i][j] = dp[i-1][j]; if(strcmp(s[i-1][j],s[i][j-1])>0) strcpy(s[i][j],s[i][j-1]); else strcpy(s[i][j],s[i-1][j]); } } } printf("Case %d: ",++cas); i--; j--; if(dp[i][j] == 0)//长度 printf(":(\n"); else printf("%s\n",s[i][j]); } return 0;}
- LightOJ 1110 - An Easy LCS(最长公共子序列 路径输出 三维 模板啊)
- lightoj 1110 - An Easy LCS 最长公共子序列输出路径
- lightoj 1110 - An Easy LCS 最长公共子序列+string
- LightOJ 1110 An Easy LCS LCS路径输出
- lightoj 1110 - An Easy LCS (LCS输出路径)
- 【51node】-1006- 最长公共子序列Lcs(LCS,输出序列,模板)
- 最长公共子序列LCS模板
- 最长公共子序列(LCS)模板
- lightoj - 1033 - Generating Palindromes - dp/ 最长公共子序列lcs
- LCS 最长公共子序列并输出该序列
- LightOj 1110 An Easy LCS
- LightOJ 1110 An Easy LCS
- LCS:最长公共子序列
- LCS---最长公共子序列
- 最长公共子序列 LCS
- LCS -- 最长公共子序列
- LCS最长公共子序列
- 最长公共子序列LCS
- 黑马程序员----Object类的eguals()和toString()方法
- 字符串与指针
- 用c语言实现 将src指向的字符串追加到dest指向字符串的后面
- hdu2072单词数 字典树做法
- JavaScript权威指南之chap6对象
- LightOJ 1110 - An Easy LCS(最长公共子序列 路径输出 三维 模板啊)
- 冒泡排序法 PHP JAVA 版本(面试必备算法)
- Codeforces_round297_B.Pasha and String
- android progressbar 使用自定义图片时的左右两端圆角效果实现
- 黑马程序员-----JAVA基础--数组排序的两种算法
- iOS-UIAlertView弹框2秒后自动消失
- (总结篇)Android 牛不牛?决定于自定义View控件(一)——view绘制流程(onMeasure,onLayout,onDraw)
- hdu 1171 Big Event in HDU
- Number of 1 Bits