ACM程序设计选修课——1018: Common Subsequence
2016-03-15 19:03
405 查看
问题 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 abfcab programming contest abcd mnp
样例输出
4 2 0
..在大神的指点下,似乎明白了点啥。智商需要充值。假设这俩字符串为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; }
相关文章推荐
- iOS常见错误8-Missing iOS Distribution signing identity for “XXXXX”. Xcode can request one for you.
- UILabel 相关使用方法间距、段落、文字颜色(详细)
- How to Build Distributed Concurrent System(思维草稿)
- Analyzing UI Performance with Systrace
- mysql Column 'dbvalue' in where clause is ambiguous解决方案
- Android Handler处理机制 ( 三 ) ——Handler,Message,Looper,MessageQueue
- MySQL提示:The server quit without updating PID file问题的解决办法
- IOS学习之在NSURLRequest中设置cookies
- UIPickerView控件中自定义 及 改变当前选中的Item的字体颜色
- iOS 特殊类 UIControl
- Java实现的Sequence工具
- WebApi设置SessionState为Required
- 关于serialVersionUID的说明
- POJ 3709 K-Anonymous Sequence
- 2016-03-15 uitextview placehold
- iOS 8 中UIAlertController使用
- Android-UI层的三种框架模式-MVC、MVP、MVVM
- Ultra-QuickSort
- iOS UIView 一些属性方法总结
- Get value from agent failed: cannot connect to [[192.168.1.110]:10050]: [113] No route to host