zoukankan      html  css  js  c++  java
  • 题目1042:Coincidence

    题目描述:

    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
     1 import java.util.Scanner;
     2  
     3  
     4 public class Main{
     5     public static void main(String[]args){
     6     Scanner in=new Scanner(System.in);
     7     while(in.hasNext()){
     8         String x=in.nextLine();
     9         String y=in.nextLine();
    10         int lenx=x.length();
    11         int leny=y.length();
    12         int[][]F=new int[lenx+1][leny+1];
    13         for(int i=0;i<=lenx;i++) F[i][0]=0; 
    14         for(int i=0;i<=leny;i++) F[0][i]=0;
    15         for(int i=1;i<=lenx;i++){
    16         char cx=x.charAt(i-1);
    17         for(int j=1;j<=leny;j++){
    18             char cy=y.charAt(j-1);
    19             if(cx==cy){
    20             F[i][j]=F[i-1][j-1]+1;
    21             }
    22             else{
    23             F[i][j]=Math.max(F[i-1][j],F[i][j-1]);
    24             }
    25         }
    26         }
    27         System.out.println(F[lenx][leny]);
    28     }
    29     }
    30  }
    31  
    32 /**************************************************************
    33     Problem: 1042
    34     User: 0000H
    35     Language: Java
    36     Result: Accepted
    37     Time:80 ms
    38     Memory:15432 kb
    39 ****************************************************************/
  • 相关阅读:
    linux查询php.ini位置
    laravel打印完整SQL语句
    python识别图片中的文字
    python -使用pytesseract识别文字时遇到的问题
    python弹出选择文件的弹出窗获取文件方法
    python将字符串中多个空格换为一个空格
    python生成word文档
    linux下tar命令
    python使用xpath获取内容
    正则表达式匹配空行
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4508175.html
Copyright © 2011-2022 走看看