Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

也是先解LCS,再倒推回去得到合并序列,0msAC,不知道下面的为什么TLE(附代码)

Posted by On18 at 2020-04-21 12:03:10 on Problem 2264
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string s1, s2;
    while (cin >> s1 >> s2)
    {
        int n1 = s1.length();
        int n2 = s2.length();
        vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1));
        vector<int> a1(n1 + 1);
        vector<int> a2(n2 + 1);
        for (int i = 1; i <= n1; ++i)
            for (int j = 1; j <= n2; ++j)
            {
                if (s1[i - 1] == s2[j - 1])
                {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        int i = n1, j = n2;
        vector<char> ans;
        while(i > 0 && j > 0)
        {
            if(s1[i - 1] == s2[j - 1])
            {
                ans.push_back(s1[i - 1]);
                i -= 1, j -= 1;
            }
            else if(dp[i][j] == dp[i - 1][j])
                {
                    ans.push_back(s1[i - 1]);
                    i -= 1;
                }
            else if(dp[i][j] == dp[i][j - 1])
                {
                    ans.push_back(s2[j - 1]);
                    j -= 1;
                }
        }
        while(i > 0)
        {
            ans.push_back(s1[i - 1]);
            i--;
        }
        while(j > 0)
        {
            ans.push_back(s2[j - 1]);
            j--;
        }
        for (int i = ans.size() - 1; i >= 0;--i)
            cout << ans[i];
        cout << endl;
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator