HDU1159——Common Subsequence
来源:互联网 发布:测试交换机端口带宽 编辑:程序博客网 时间:2024/06/05 21:50
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35297 Accepted Submission(s): 16111
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.
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 abfcabprogramming contest abcd mnp
Sample Output
420
Source
Southeastern Europe 2003
题意:
给出两个字符串,求两个字符串的最长公共字串的长度。
解:
动态规划基础题目。
if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=(dp[i][j-1]>p[j][i-1]?dp[i][j-1]:p[j][i-1]);
#include <stdio.h>#include <string.h> int a[1000][1000];int b[1000][1000];void LCSlength(int m,int n,char x[],char y[]){ int i,j; for(i=1;i<=m;i++) a[i][0]=0; for(i=1;i<=n;i++) a[0][i]=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(x[i]==y[j]){ a[i+1][j+1]=a[i][j]+1; b[i+1][j+1]=1; }else if(a[i][j+1]>=a[i+1][j]){ a[i+1][j+1]=a[i][j+1]; b[i+1][j+1]=2; } else{ a[i+1][j+1]=a[i+1][j]; b[i+1][j+1]=3; } } }} int main(){ char x[1000]; char y[1000]; char z[10000]; int m,n; while(gets(z)) { memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); for(m=0;m<strlen(z);m++) { if(z[m]!=' ') x[m]=z[m]; else break; } int i=0; for(n=m+1;n<strlen(z);n++) { if(z[n]!=' ') y[i++]=z[n]; } LCSlength(m,i+1,x,y); printf("%d\n",a[m][i+1]); } return 0; }
0 0
- HDU1159——Common Subsequence
- HDU1159——Common Subsequence
- 【hdu1159】common subsequence —— 二维dp
- 【DP复习4—LCS】HDU1159——Common Subsequence
- ACM-DP之Common Subsequence——HDU1159
- HDU1159 Common Subsequence (LCS)
- HDU1159--Common Subsequence
- hdu1159-Common Subsequence
- hdu1159 Common Subsequence
- HDU1159 Common Subsequence
- HDU1159:Common Subsequence
- hdu1159 Common Subsequence DP
- HDU1159 Common Subsequence
- HDU1159,Common Subsequence,toCharArray()
- HDU1159:Common Subsequence(LCS)
- HDU1159 Common Subsequence
- hdu1159 Common Subsequence(LCS)
- HDU1159---Common Subsequence
- 11.23
- 简单工厂模式
- leetcode(38).141. Linked List Cycle
- 什么是MVC
- leetcode 455. Assign Cookies
- HDU1159——Common Subsequence
- Android开发:filePath放在哪个文件夹
- c语言里如何调用汇编里的变量?
- nyoj 206 矩形个数
- PHP static后期静态绑定
- 例题5-8 Unix ls 命令 UVa400
- swing带标题的边框
- 最大子列和(二)
- XlistView 上拉加载,下拉刷新