杭电acm 1159Common Subsequence(字符串处理+dp)
来源:互联网 发布:人工智能四大平台 编辑:程序博客网 时间:2024/06/08 11:16
Common Subsequence
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
想法:简单题
注意细节
代码:
#include<stdio.h>
#include<string.h>
char s[1100];
char s1[1100];
int dir[1100][1100];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
while(scanf("%s %s",s,s1)!=EOF)
{
memset(dir,0,sizeof(dir));
int len=strlen(s);
int len1=strlen(s1);
int i,j;
for(i=1;i<=len;i++)
{
for(j=1;j<=len1;j++)
{
if(s[i-1]==s1[j-1])
dir[i][j]=dir[i-1][j-1]+1;
else
dir[i][j]=max(dir[i-1][j],dir[i][j-1]);
}
}
printf("%d\n",dir[len][len1]);
}
return 0;
}
#include<string.h>
char s[1100];
char s1[1100];
int dir[1100][1100];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
while(scanf("%s %s",s,s1)!=EOF)
{
memset(dir,0,sizeof(dir));
int len=strlen(s);
int len1=strlen(s1);
int i,j;
for(i=1;i<=len;i++)
{
for(j=1;j<=len1;j++)
{
if(s[i-1]==s1[j-1])
dir[i][j]=dir[i-1][j-1]+1;
else
dir[i][j]=max(dir[i-1][j],dir[i][j-1]);
}
}
printf("%d\n",dir[len][len1]);
}
return 0;
}
阅读全文
0 0
- 杭电acm 1159Common Subsequence(字符串处理+dp)
- HDOJ 1159 Common Subsequence 杭电 ACM
- 杭电 HDU ACM 1159 Common Subsequence
- 杭电1159(Common Subsequence)LCS和dp
- 杭电1159-Common Subsequence
- 杭电1159Common Subsequence
- 杭电-1159 Common Subsequence(最长公共子序列)
- 杭电OJ 1159 :Common Subsequence
- 杭电1159—Common Subsequence题解
- 杭电-1159Common Subsequence(LCS)
- Common Subsequence(DP)
- 【杭电oj1159】Common Subsequence
- HDU 1159 Common Subsequence(还是DP)
- hdu 题目1159 Common Subsequence(DP)
- HDU 1159 Common Subsequence(dp LCS)
- HDU 1159 Common Subsequence (备忘录DP)
- hdu 1159 Common Subsequence(dp)
- hdu 1159 Common Subsequence (dp)
- 我和你
- Mac电脑传输文件慢的原因
- 其实就是测试一下,第一个博客
- C8051F330基本外设调试
- HDU6055空间内n个点能组成多少个正方形
- 杭电acm 1159Common Subsequence(字符串处理+dp)
- struts2.3升级到struts2.5.10.1步骤及遇到的问题
- A+B Problem(V)
- Mysql主从配置(超简单的)
- Laravel ORM操作
- http协议
- curl网站开发指南
- myabtis 查询时间段
- sort 排序 list 崩溃的问题