HDU1159Common Subsequence(dp)
2015-08-24 12:21
441 查看
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29009 Accepted Submission(s): 12992
Problem Description
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.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
Source
Southeastern Europe 2003
Recommend
Ignatius | We have carefully selected several similar problems for you: 1069 2084 1203 1160 2602
题意:求两个字符串的最大公共子序列长度,可以不连续。
思路:dp,用过string函数中的strchr(a,ch)可惜会wa
用过string函数中的strchr(a,ch)可惜会wa,数据能过
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29009 Accepted Submission(s): 12992
Problem Description
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.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
Source
Southeastern Europe 2003
Recommend
Ignatius | We have carefully selected several similar problems for you: 1069 2084 1203 1160 2602
题意:求两个字符串的最大公共子序列长度,可以不连续。
思路:dp,用过string函数中的strchr(a,ch)可惜会wa
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; char a[1005]; char b[1005]; int dp[1005][1005]; int max(int a,int b) { return a>b?a:b; } int main() { while(~scanf("%s%s",a,b)) { memset(dp,0,sizeof(dp)); int len1=strlen(a); int len2=strlen(b); for(int i=1;i<=len1;i++) { for(int j=1;j<=len2;j++) { if(a[i-1]==b[j-1]) { dp[i][j]=dp[i-1][j-1]+1; } else { dp[i][j]=max(dp[i][j-1],dp[i-1][j]); } } } printf("%d\n",dp[len1][len2]); } return 0; }
用过string函数中的strchr(a,ch)可惜会wa,数据能过
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; char a[100005]; char b[100005]; char c; int visit[105]; int main() { int t,len; while(~scanf("%s%s",a,b)) { t=0; len=strlen(b); memset(visit,0,sizeof(visit)); for(int i=0; i<len; i++) { if(!visit[b[i]-'a']) { c=b[i]; if(strchr(a,c)) t++; visit[b[i]-'a']=1; } } printf("%d\n",t); } }
相关文章推荐
- iOS UITableView单元格单击出现延迟,双击正常执行事件问题
- FragmentTabHost切换Fragment时避免UI重新加载
- 关于UIScrollView的alwaysBounceVertical属性
- A1099. Build A Binary Search Tree (30)
- 基于MVC4+EasyUI的Web开发框架经验总结(4)--使用图表控件Highcharts
- android 系统属性 build.prop
- 串口下载uboot和uImage
- HDU 4667 Building Fence(求凸包的周长)
- Unknown Class **** in Interface Builder file
- NSIS un.onGUIInit 重复定义解决
- UIImageView 的contentMode属性
- <c:if test="value ne, eq, lt, gt,...."> 用法
- easyui 中 OnClickRow 的使用案例
- 将DataTable转换成EasyUITree的json格式 ---四级树
- MockupBuilder
- android UI之ScaleAnimation解析
- 计算UITableViewCell之间的间距
- IOS UI 之 去tabelView分割线
- 2015-08-24 part1 开机配置+buildroot-2012.08
- Easyui Datebox设置可选时间范围