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

[AC]感觉有点像最回文子串和编辑距离的结合,二重循环,dp解法

Posted by On18 at 2020-04-15 16:31:45 on Problem 3280
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int dp[2008][2008];

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> cost(26);
    string s;
    cin >> s;
    char c;
    int a, b;
    for (int i = 0; i < n; ++i)
    {
        cin >> c >> a >> b;
        cost[(c - 'a')] = min(a, b);
    }
    int j;
    for (int k = 1; k < m; ++k)
    {
        for (int i = 0; i < m - 1; ++i)
        {
            j = i + k;
            if (j < m)
            {
                dp[i][j] = min(dp[i + 1][j] + cost[s[i] - 'a'], dp[i][j - 1] + cost[s[j] - 'a']);
                if (s[i] == s[j])
                    dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
            }
        }
    }
    cout << dp[0][m - 1] << 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