ACM程序设计选修课——1018: Common Subsequence
来源:互联网 发布:山东seo公司 编辑:程序博客网 时间:2024/05/02 18:33
问题 L: Common Subsequence
时间限制: 1 Sec 内存限制: 32 MB提交: 70 解决: 40
[提交][状态][讨论版]
题目描述
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
输入
输出
样例输入
abcfbc abfcabprogramming contest abcd mnp
样例输出
420
..在大神的指点下,似乎明白了点啥。智商需要充值。假设这俩字符串为a与bC[i][j]来表示当前移动到a的第i个位置,b的第j个位置时所产生的最大公共子字符个数。
C[i][j]要么保持上两个状态中的一个,要么出现了公共的字符进行+1操作。
list[i+1][j+1]=(a[i]==b[j] ? list[i][j]+1 : max( list[i+1][j] , list[i][j+1]) );
代码:
#include<iostream>#include<string>#include<algorithm>#include<map>#include<set>#include<cstring>#include<cmath>using namespace std;int list[1010][1010];int main(void){int t,i,j,ans;string a,b;while (cin>>a>>b){memset(list,0,sizeof(list));int la=(int)a.size();int lb=(int)b.size();for (i=0; i<la; i++){for (j=0; j<lb; j++){list[i+1][j+1]=(a[i]==b[j]?list[i][j]+1:max(list[i+1][j],list[i][j+1]));//状态转移方程}}printf("%d\n",list[la][lb]);}return 0;}
- ACM程序设计选修课——1018: Common Subsequence
- ACM程序设计选修课——1049: Efface Numbers(贪心)
- ACM程序设计选修课——1058: Lucky Sequence(思考)
- ACM程序设计选修课——1081: 堆(BFS)
- ACM程序设计选修课——1030: Hungar的时尚球场(水题+耐心)
- ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)
- ACM程序设计选修课——1051: Glamor Sequence(YY+求和公式)
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
- ACM程序设计选修课——1065: Operations on Grids(暴力字符串)
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
- ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)
- ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)
- ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
- ACM-DP之Common Subsequence——HDU1159
- [ACM]Common Subsequence
- [ACM]Common Subsequence
- Hadoop的那些事儿
- Mathematical Foundations
- 第三周项目四-穷举法解决组合问题(1)
- Qt中QSlider的样式表设置
- 一个应用程序无法启动错误的解决过程
- ACM程序设计选修课——1018: Common Subsequence
- AndroidAnnotations 是如何工作的
- HDU Buildings
- Zzz读书心得:英文论文写作不求人
- JAVA_get和set方法
- 蓝桥杯 牌型种数 dp解法
- Eclipse自动补全+常用快捷键
- hdu3018并查集+欧拉回路
- isPostBack