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 spent ci 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 string B if it is shorter than B (|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 in A is smaller than the character in B.
For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.
Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.
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.
2 1 2 ba ac
1
3 1 3 1 aa ba ac
1
2 5 5 bbb aaa
-1
2 3 3 aaa aa
-1
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 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.
一道动态规划的题。题目大意:给出n个字符串,反转每个字符串需要ci 单位的能量,保证这n个字符串是lexicographical的。怎样才算是lexicographical呢:字符串A的长度要小于B的长度,且A是B的前缀,或者A和B一样长,但是 at the first position where they differ character in A is smaller than the character in B.
官方题解:We will solve the problem with the help of dynamic programming. dp[i][j] is the minimum amount of energy that should be spent to make first i strings sorted in lexicographical order and i-th of them will be reversed if j = 1 and not reversed if j = 0. dp[i][j] is updated by dp[i - 1][0] and dp[i - 1][1]. It remains to verify that the i-th string is lexicographically greater than (i - 1)-th (if j = 1 then we should check reversed i-th string, similar to (i - 1)-th). Then we update dp[i][j] = min(dp[i][j], dp[i - 1][0] + c[i] * j), dp[i][j] = min(dp[i][j],dp[i - 1][1] + j * c[i]). The answer is a minimum of dp[n][0] and dp[n][1].
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
string s[maxn];
int c[maxn], n;
long long dp[2][maxn];
int main()
{
while(cin >> n)
{
for(int i = 0; i < n; i++)
cin >> c[i];
for(int i = 0; i < n; i++)
cin >> s[i];
long long INF = (long long)1e18;
for(int i = 0; i < n; i++)
dp[0][i] = INF, dp[1][i] = INF;
dp[0][0] = 0;
dp[1][0] = c[0];
for(int i = 1; i < n; i++)
{
string t1 = s[i-1], t2 = s[i];
reverse(t1.begin(), t1.end());
reverse(t2.begin(), t2.end());
if (s[i] >= s[i - 1])
dp[0][i] = dp[0][i - 1];
if (s[i] >= t1)
dp[0][i] = min(dp[1][i - 1], dp[0][i]);
if (t2 >= s[i - 1])
dp[1][i] = min(c[i] + dp[0][i - 1], dp[1][i]);
if (t2 >= t1)
dp[1][i] = min(c[i] + dp[1][i - 1], dp[1][i]);
}
long long ans = min(dp[0][n-1], dp[1][n-1]);
if(ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}