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;
    }
  • 相关阅读:
    【翻译】让你的网站飞起来
    理解ASP.NET MVC中的模型绑定
    【转载】创建定制ASP.NET MVC视图引擎
    修改STM32主频
    Cortex系列ARM核心及体系结构介绍
    递归
    NFD模拟兴趣包的转发
    NX 笔记
    MicroPython 8266 配置
    Python JSON操作
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3913294.html
Copyright © 2011-2022 走看看