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;
}

CSU 1060,布布扣,bubuko.com

时间: 2024-12-21 04:30:55

CSU 1060的相关文章

CSU 1060 Nearest Sequence

题意:求三个序列的最长公共子序列. 思路:一开始以为只要求出前两个的LCS,然后和第三个再求一遍LCS就是答案了.但是样例就对我进行啪啪啪打脸了.实际上就跟两个序列的差不多,换成三维的就行了. 代码:需要注意的是max速度比较慢,最后改成if 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 using namespace std; 5 const int N=111; 6 int dp[N]

CSU-ACM2014年校队选拔赛指导赛解题报告

•Problem A  CSU 1065                               贪心 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1000010; 6 struct Node{ 7 int a,b; 8 bool operator < (const Node& rhs)

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50

CSU 1112: 机器人的指令【模拟题】

1112: 机器人的指令 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1858  Solved: 682 [Submit][Status][Web Board] Description 数轴原点有一个机器人.该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置. ·LEFT:往左移动一个单位 ·RIGHT: 往右移动一个单位 ·SAME AS i: 和第i 条执行相同的动作.输入保证i 是一个正整数,且不超过之前执行指令数 In

CSU 1416 Practical Number

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1416 结论题,具体判断方法请点击这个网址. 筛素数是肯定的,但一开始定的范围太大了,想当然要筛到10^9的质数,但仔细想想,只要到sqrt(10^9)就可以了,最后的那一个质数是最后一步的比较,不用筛出来. #include <stdio.h> #include <string.h> #include <iostream> using namespace st

CSU 1412 Line and Circles

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1412 题目要求判断是否有一条直线可以穿过所有的圆. 做法:把所有圆心做一次凸包,然后判断这个凸包是否能通过一个宽度为2*R的通道. 做法和求凸包直径差不多,只是判断的时候把点到两个端点的距离换成点到直线的距离. #include <stdio.h> #include <string.h> #include <math.h> #include <stdli

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th

【BZOJ 1060】 1060: [ZJOI2007]时态同步 (树形DP)

1060: [ZJOI2007]时态同步 Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 存在一条通路(通路指连接两个元件的导线序列).在电路板上存在一个特殊的元件称为"激发器".当激发器工 作后,产生一个激励电流,通过导线传向每一个它所连接的节点.而中间节点接收到激励电流后,得到信息,并将 该激励电流传向