题目1042:Coincidence(2008年上海交通大学计算机研究生机试真题)
2014-06-23 20:28
387 查看
题目1042:Coincidence
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1399
解决:748
题目描述:
Find a longest common subsequence of two strings.
输入:
First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.
输出:
For each case, output k – the length of a longest common subsequence in one line.
样例输入:
样例输出:
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1399
解决:748
题目描述:
Find a longest common subsequence of two strings.
输入:
First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.
输出:
For each case, output k – the length of a longest common subsequence in one line.
样例输入:
abcd cxbydz
样例输出:
2
import java.util.Scanner; public class Main{ static int dp[][] = new int [101][101]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while( scanner.hasNext() ){ String s1 = scanner.next(); String s2 = scanner.next(); int l1 = s1.length(); int l2 = s2.length(); char charOfs1[] = s1.toCharArray(); char charOfs2[] = s2.toCharArray(); for (int i = 0; i <= l1; i++) { dp[i][0] = 0; } for (int i = 0; i <= l2; i++) { dp[0][i] = 0; } for (int i = 1; i <= l1; i++) { for (int j = 1; j <= l2; j++) { if(charOfs1[i-1] != charOfs2[j-1]){ dp[i][j] = max(dp[i][j-1],dp[i-1][j]); } else{ dp[i][j] = dp[i-1][j-1] +1; } } } System.out.println(dp[l1][l2]); } } private static int max(int a, int b) { return a>b?a:b; } } /************************************************************** Problem: 1042 User: yihukurama Language: Java Result: Accepted Time:80 ms Memory:15456 kb ****************************************************************/
相关文章推荐
- 九度题目1042:Coincidence 2008年上海交通大学计算机研究生机试真题
- 九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题
- 题目1026:又一版 A+B(2008年浙江大学计算机及软件工程研究生机试真题)
- 题目1118:数制转换(2008年北京大学图形实验室计算机研究生机试真题)
- 题目1027:欧拉回路 2008年浙江大学计算机及软件工程研究生机试真题
- 九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题
- 题目1163:素数 2008年北京航空航天大学计算机研究生机试真题
- 题目1163:素数(2008年北京航空航天大学计算机研究生机试真题)
- 题目1080:进制转换(2008年清华大学计算机研究生机试真题)ps:用java太简单了
- 题目1012:畅通工程(2005年浙江大学计算机及软件工程研究生机试真题)
- 简单的素数问题(2008年北京航空航天大学计算机研究生机试真题)
- 题目1014:排名 2005年浙江大学计算机及软件工程研究生机试真题
- 题目1018:统计同成绩学生人数(哈希算法,2006年浙江大学计算机及软件工程研究生机试真题)
- 题目1076:N的阶乘(2006年清华大学计算机研究生机试真题)
- 题目1112:拦截导弹(2007年北京大学计算机研究生机试真题:动态规划)
- 题目1051:数字阶梯求和 2010年哈尔滨工业大学计算机研究生机试真题
- 题目1153:括号匹配问题(2010年北京大学计算机研究生机试真题,堆栈的使用)
- 题目1083:特殊乘法(2010年清华大学计算机研究生机试真题)
- 题目1045:百鸡问题(2009年哈尔滨工业大学计算机研究生机试真题)
- 九度题目1154:Jungle Roads Kruskal算法 2010年北京大学计算机研究生机试真题