标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 46630 | Accepted: 19154 |
Description
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
Java AC 代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String first = ""; String second = ""; while(!(first = sc.next()).equals("") && !(second = sc.next()).equals("")) { char[] firstArray = first.toCharArray(); char[] secondArray = second.toCharArray(); int firstLen = first.length(); int secondLen = second.length(); int[][] subMaxLen = new int[firstLen + 1][secondLen + 1]; //这里设置成长度加1的,是为了防止下面 i-1 j-1的时候数组越界。 for(int i = 1; i <= firstLen; i++) for(int j = 1; j <= secondLen; j++) { if(firstArray[i - 1] == secondArray[j - 1]) subMaxLen[i][j] = subMaxLen[i - 1][j - 1] + 1; else subMaxLen[i][j] = (subMaxLen[i - 1][j] > subMaxLen[i][j - 1] ? subMaxLen[i - 1][j] : subMaxLen[i][j - 1]); } System.out.println(subMaxLen[firstLen][secondLen]); } } }
poj 1458 Common Subsequence(dp)
标签:
原文地址:http://www.cnblogs.com/kkkkkk/p/5545307.html