首页 > 其他分享 >Educational DP Contest F - LCS(最长公共子序列)

Educational DP Contest F - LCS(最长公共子序列)

时间:2022-12-23 09:34:17浏览次数:61  
标签:Educational LCS Contest -- LL cout cin mp size

https://atcoder.jp/contests/dp/tasks/dp_f

题目大意:

给定字符串s和c(1<=s,c<=3000),求最长公共子序列的具体字符串。
Sample Input 1  
axyb
abyxb
Sample Output 1  
axb

正解:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=200200,M=3020;
LL f[M][M];
string mp[M][M];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        string s,c;
        cin>>s>>c;
        LL n=s.size(),m=c.size();
        s=" "+s;
        c=" "+c;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                f[i][j]=max(f[i-1][j],f[i][j-1]);
                if(s[i]==c[j])
                {
                    f[i][j]=max(f[i][j],f[i-1][j-1]+1);
                }
            }
        }
        //cout<<f[n][m]<<endl;
        string ans="";
        LL i=n,j=m;
        while(i>0&&j>0)
        {
            if(s[i]==c[j])
            {
                ans+=s[i];
                i--;
                j--;
            }
            else
            {
                //直接找向更多更长相等字符串的位置
                if(f[i-1][j]>f[i][j-1]) i--;
                else j--;
            }
        }
        //因为是倒着找的,所以我们需要对答案再倒一下
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
    }
    return 0;
}

但是我不懂为啥这个写法会被t和re?

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=200200,M=3020;
LL f[M][M];
string mp[M][M];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        string s,c;
        cin>>s>>c;
        LL n=s.size(),m=c.size();
        s=" "+s;
        c=" "+c;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(f[i-1][j]>f[i][j-1]) mp[i][j]=mp[i-1][j];
                else mp[i][j]=mp[i][j-1];

                f[i][j]=max(f[i-1][j],f[i][j-1]);
                if(s[i]==c[j])
                {
                    if(f[i-1][j-1]+1>f[i][j]) mp[i][j]=mp[i-1][j-1]+s[i];
                    f[i][j]=max(f[i][j],f[i-1][j-1]+1);
                }
            }
        }
        //cout<<f[n][m]<<endl;
        cout<<mp[n][m]<<endl;
    }
    return 0;
}

标签:Educational,LCS,Contest,--,LL,cout,cin,mp,size
From: https://www.cnblogs.com/Vivian-0918/p/17000007.html

相关文章

  • Educational DP Contest E - Knapsack 2 (01背包)
    https://atcoder.jp/contests/dp/tasks/dp_e题目大意:有N个物品,编号为1,2,…,N。对于每个i(1≤i≤N),物品I的权重为wi,价值为vi。Taro决定从N件物品中挑选一些,用背包带回家......
  • Educational Codeforces Round 139 D. Lucky Chains
    LuckyChains题面翻译给定两个数·a,b,(a,b给到了1e7)执行如下语句:while(gcd(a,b)==1)a++,b++,cnt++;求出cnt的值。样例#1样例输入#145151337891......
  • AtCoder Beginner Contest 282
    https://atcoder.jp/contests/abc282A-GeneralizedABC原题链接题意给出一个数\(k\),输出A~Z中的前\(k\)个字母。分析从\(0\)到\(k\)枚举,将A加上\(i\)输......
  • JAG Spring Contest 2012 G PLAY in BASIC 题解
    提交链接其实就是个大模拟。首先对输入的串进行处理,把所有的命令分开,并把连续的停顿合并。为了方便,定义一个时间单位为全音符的\(\frac1{128}\),这样所有命令的持续时间都......
  • JAG Spring Contest 2012 G PLAY in BASIC 题解
    提交链接其实就是个大模拟。首先对输入的串进行处理,把所有的命令分开,并把连续的停顿合并。为了方便,定义一个时间单位为全音符的\(\frac1{128}\),这样所有命令的持续时间都......
  • AtCoder Beginner Contest 282
    《MakeBipartite2》思维,二分图  这个简单图可以有两种情况:1.全部点都通过边连起来,即连通分量只有一个,其自己2.还有有些点没有全部连起来,即有多个连通分......
  • Educational Codeforces Round 140 (Rated for Div. 2)
    A题意:给定二维坐标的三个顶点构成一个三角形。请问能否用一条平行于坐标轴的线段将三角形分割成两个非退化的三角形。核心思路:只有一种情况是无法分割的,那就是是一个直......
  • (AtCoder Beginner Contest 282) D - Make Bipartite 2(二分图)
    题目大意:给定一个n个点m条边的图,请你在其中加一条边使得整个图G是二分图,问有多少种可能。(已有的边不能加)解题思路:首先我们要知道,二分图内是没有长度为奇数的回路的所......
  • AtCoder Beginner Contest 282
    A-GeneralizedABC(abc282a)题目大意给定\(n\),输出一个字符串,从'A','B','C'...拼接得到的长度为\(n\)。解题思路模拟即可。神奇的代码#include<bits/stdc++.h......
  • AtCoder Beginner Contest 281 (A-D)
    A题意:输入一个整数,输出(n+1)行,从n一直输出到0.解法/思路:一个循环完事儿。代码:#include<iostream>usingnamespacestd;intmain(){ intn; cin>>n; for(in......