zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    Common Subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 19679    Accepted Submission(s): 8389


    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. 
     
    Sample Input
    abcfbc abfcab
    programming contest
    abcd mnp
     
    Sample Output
    4
    2
    0
     
      题意就是求两个字符串的最长公共子串的长度。这题可以算是LCS的例题= =,所以直接写一个求两个字符串的LCS就可以了,但是这里没有告诉我们字符串的长度,所以字符串数组开了1000+,所以用来记录LCS的数组开不了这么大,只好用滚动数组,在原本的LCS上面稍微改一下就可以了。
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #define MAX 1100
     4 using namespace std;
     5 
     6 char a[MAX],b[MAX];
     7 int c[11][MAX];
     8 
     9 int main()
    10 {
    11     int lena,lenb,maxn,v;
    12     while(scanf("%s %s",a+1,b+1)!=EOF){
    13         lena=strlen(a+1);
    14         lenb=strlen(b+1);
    15         memset(c,0,sizeof(c));
    16         maxn=0;
    17         v=1;
    18         for(int i=1;i<=lena;i++){
    19             v=i%10;                 //这一句需要放在外面的循环里
    20             for(int j=1;j<=lenb;j++){
    21                 if(a[i]==b[j]) c[v][j]=c[(v-1+10)%10][j-1]+1;
    22                 else if(c[v][j-1]>=c[(v-1+10)%10][j]){
    23                     c[v][j]=c[v][j-1];
    24                 }else{
    25                     c[v][j]=c[(v-1+10)%10][j];
    26                 }
    27                 maxn = c[v][j] > maxn ? c[v][j] : maxn;
    28             }
    29         }
    30         printf("%d
    ",maxn);
    31 
    32     }
    33     return 0;
    34 }
    35 
    36 
    37 /**
    38 abcfbc abfcab
    39 programming contest
    40 abcd mnp
    41 
    42 4
    43 2
    44 0
    45 */
    1159
     
      
  • 相关阅读:
    对于学习 ionic的同学安装的时候肯定很痛苦 get 一个小技巧
    关于Android sdk有事安装出现报Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml错误的问题 只要两部就可以搞定
    很多同学工作了,这里呢简单说说工作中吧可能会遇到的一些事情‘
    css定位机制
    HBuilder简单操作
    前端学习方法小结
    Ajax基础讲解 1
    css hack
    jQuery自定义滚动条样式插件mCustomScrollbar 兼容IE7的方法
    ie下面兼容性问题的一些总结(转)
  • 原文地址:https://www.cnblogs.com/sineatos/p/3522779.html
Copyright © 2011-2022 走看看