zoukankan      html  css  js  c++  java
  • CSU 1060

    1060: Nearest Sequence

    Time Limit: 1 Sec  Memory Limit: 64 MB
    Submit: 370  Solved: 118
    [Submit][Status][Web Board]

    Description

            Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of char,tell me the length of the longest common subsequence of the three sequences.

    Input

            There are several test cases.For each test case,the first line gives you the first sequence,the second line gives you the second one and the third line gives you the third one.(the max length of each sequence is 100)

    Output

     

     

            For each test case,print only one integer :the length of the longest common subsequence of the three sequences.

    Sample Input

    abcd
    abdc
    dbca
    abcd
    cabd
    tsc

    Sample Output

    2
    1

    HINT

     

    Source

    CSU Monthly 2012 May.

    题目没有读懂

    这个题目的意思和最长上升子序列是一样的

    不要求连续

    看了一下AC代码

    用dp写的

    意思也很明白

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    const int maxn = 105;
    char s1[maxn],s2[maxn],s3[maxn];
    int dp[maxn][maxn][maxn];
    
    int main(){
        string s1,s2,s3;
        while(cin>>s1){
            cin>>s2>>s3;
            memset(dp,0,sizeof(dp));
            //三重循环
            for(int i = 1;i <= s1.length();i++)
            {
                for(int j = 1;j <= s2.length();j++)
                {
                    for(int k = 1;k <= s3.length();k++)
                    {
                        if(s1.at(i-1) == s2.at(j-1) && s2.at(j-1) == s3.at(k-1))
                        {
                            dp[i][j][k] = dp[i-1][j-1][k-1]+1;//三个位置相同
                        }
                        else
                        {
                            int tmp1 = dp[i-1][j][k];
                            int tmp2 = dp[i][j-1][k];
                            int tmp3 = dp[i][j][k-1];
                            int ans = max(tmp1,tmp2);//不同则为之前保存的最大值
                            ans = max(ans,tmp3);
                            dp[i][j][k] = ans;
                        }
                    }
                }
            }
    
            cout<<dp[s1.length()][s2.length()][s3.length()]<<endl;;
        }
        return 0;
    }
  • 相关阅读:
    webservice的cxf的客户端
    webservice用cxf发布SOAP
    webservice声明发布SOAP1.2
    webservice使用注解修改WSDL内容
    webservice获取天气信息
    结巴分词原理介绍
    Pytorch学习记录-torchtext和Pytorch的实例( 使用神经网络训练Seq2Seq代码)
    【Pandas】Pandas求某列字符串的长度,总结经验教训
    UTF-8与UTF-8 BOM
    logging.basicConfig函数
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3913294.html
Copyright © 2011-2022 走看看