codefoces 706C Hard problem (dp/dfs)

本文解析了CodeForces C题目的解决方案,采用动态规划方法求解字符串排序问题,通过反转字符串并计算最小能量消耗来达到字典序排序的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Hard problem

Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.

Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).

To reverse the i-th string Vasiliy has to spentci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.

String A is lexicographically smaller than stringB if it is shorter thanB (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character inA is smaller than the character inB.

For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

Input

The first line of the input contains a single integern (2 ≤ n ≤ 100 000) — the number of strings.

The second line contains n integersci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse thei-th string.

Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.

Output

If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.

Examples
Input
2
1 2
ba
ac
Output
1
Input
3
1 3 1
aa
ba
ac
Output
1
Input
2
5 5
bbb
aaa
Output
-1
Input
2
3 3
aaa
aa
Output
-1
Note

In the second sample one has to reverse string2 or string3. To amount of energy required to reverse the string3 is smaller.

In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.

In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.



解题思路:我最开始想到的算法是dfs搜索,不过因为思路不太清晰,当场WA了之后就不知所措了...后来慢慢想出了dp的写法(其实两者差不太多),我开了一个dp数组,dp[ i ][ j ] ,j分为0/1两种情况——是否翻转,i则表示第几个字符串 ,dp[ i ][ 0 ] 也就表示第i个字符串未翻转并且后面所有字符串都满足条件的最小花费,dp[ i ][ 1 ] 对应第 i 个字符串翻转后的结果。所以可以得到递推式 

dp[ i ][ j ] = (j==1 ? a[i] : 0) + dp[i+1][j];

因为要分为是否翻转两种情况,所以要对两种结果取最小值,最后考虑一下递推顺序是从后往前,初始化一下

dp[n-1][0] = 0; dp[n-1][1] = a[n-1]; 。

      今天花了一天的时间调试我的记忆化搜索,没想到最后好不容易调好了,还是TLE...(T^T),不太清楚为什么记忆化搜索会T可能是常数太大了??

DP:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int a[100005];
string s[100005][2];
int n;
ll dp[100005][2];
int main()
{
    //freopen("test.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int i,j;
    cin>>n;
    for(i=0; i<n; i++)
        cin>>a[i];
    for(i=0; i<n; i++)
    {
        cin>>s[i][0];
        s[i][1] = s[i][0];
        reverse(s[i][1].begin(),s[i][1].end());
        //dp[i][0] = dp[i][1] = 1E18;
    }
    dp[n-1][0] = 0;
    dp[n-1][1] = a[n-1];
    for(i=n-2;i>=0;i--)
    {
        for(j=0;j<=1;j++)
        {
            ll temp1 = 1E18;
            ll temp2 = 1E18;
            if(s[i][j]<=s[i+1][j])
                temp1 = (j==1 ? a[i] : 0) + dp[i+1][j];
            if(s[i][j]<=s[i+1][!j])
                temp2 = (j==1 ? a[i] : 0) + dp[i+1][!j];
            dp[i][j] = min( temp1, temp2);
        }
    }
    ll res = min( dp[0][0],dp[0][1]);
    if(res>=1E18)
        cout << "-1" <<endl;
    else
        cout << res << endl;
    //cout << min( dp[0][0],dp[0][1]) << endl;
    return 0;
}

DFS:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

#define inf 1000000000000000000
using namespace std;
typedef long long ll;
int a[100005];
string s[100005][2];
int n;
ll dp[100005][2];

ll dfs(int i,int flag)
{
    //cout << s[i-1][flag] << ' ' << s[i][0] << ' ' << s[i][1] <<endl;
    if(i>n-1)
        return 0;
    if(dp[i][flag]!=inf)
        return dp[i][flag];
    if(s[i][0]<s[i-1][flag])
    {
        if(s[i][1]<s[i-1][flag])
            return dp[i][flag] = inf;//返回
        else
            dp[i][flag] = a[i] + dfs(i+1,1);
    }
    else
    {
        ll temp1 = inf;
        ll temp2 = inf;
        temp1 = dfs(i+1,0);
        if(s[i][1]>=s[i-1][flag])
            temp2 = a[i] + dfs(i+1,1);
        dp[i][flag] = min(temp1,temp2);
    }
    return dp[i][flag];
}
int main()
{
    //freopen("test.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int i;
    cin>>n;
    for(i=0; i<n; i++)
        cin>>a[i];
    for(i=0; i<n; i++)
    {
        cin>>s[i][0];
        s[i][1] = s[i][0];
        reverse(s[i][1].begin(),s[i][1].end());
        dp[i][0] = dp[i][1] = inf;
    }
    ll res1 = dfs(1,0);
    //memset(dp,0,sizeof(dp));
    ll res2 = a[0] + dfs(1,1);
    ll res = min(res1,res2);
    if(res>=inf)
        cout << "-1" <<endl;
    else
        cout << res << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值