dp--B - Hard problem

B - 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 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.

Input

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 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.

  题目大意:给定你n个字符串,以及每条字符串变动所需的代价ci。问,能否通过只对某个或某些字符串进行翻转(即头尾调换,abcde->edcba),使这n条字符串为字典序排序(短的在前,小的在前)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define LL long long 
 5 const int maxn = 100010;
 6 const LL INF=1e15;
 7 
 8 int c[maxn];
 9 string str[maxn][2];
10 LL dp[maxn][2];
11  
12 string reverse(string s){//翻转 
13     string res=s;
14     int i, len=res.length();
15     for(i=0;i<len/2;++i)
16         swap(res[i], res[len-1-i]);
17     return res;
18 }
19  
20 int main(){
21     int n, i;
22     scanf("%d",&n);
23         for(i=0;i<n;++i)
24             scanf("%d",&c[i]);
25             
26         for(i=0;i<n;++i){
27             cin>>str[i][0];
28             str[i][1]=reverse(str[i][0]);//翻转 
29         }
30         
31         for(i=0;i<n;++i)//初始化 
32             dp[i][0]=dp[i][1]=INF;
33         dp[0][0]=0; dp[0][1]=c[0];
34         
35         for(i=1;i<n;++i){
36             if(str[i][0]>=str[i-1][0])//原str i 大于 原str i-1 
37                 dp[i][0]=dp[i-1][0];
38             if(str[i][1]>=str[i-1][0])//翻转str i 大于 原str i-1 
39                 dp[i][1]=dp[i-1][0]+c[i];
40             if(str[i][0]>=str[i-1][1])//原str i 大于 翻转str i-1 
41                 dp[i][0]=min(dp[i][0],dp[i-1][1]);
42             if(str[i][1]>=str[i-1][1])//翻转str i 大于 翻转str i-1 
43                 dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
44             if(dp[i][1]==INF&&dp[i][0]==INF)
45                 break;
46         }
47         if(i==n)
48             printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
49         else
50             printf("-1\n");
51     
52     return 0;
53 }

  这是今天训练的B题,但是我却放到了后面才做,因为我看不懂题意(即使是借助了翻译软件),最后去翻了外面的博客才读懂题意,所以读懂题意是解决问题的第一条件。

  代价ci,即使告诉我这是dp专题,我却怎么都没办法联系起来,一开始我甚至觉得ci是无关紧要的;但是当我们把ci和对相邻字符串的字典序的判断联系在一起时,这个题目就非常好做了。我们只要把dpi的初始值设置的非常大或者非常小,在循环中做判断时,如果通过所谓的字符翻转,能够实现字符串的字典序排序,我就将dpi的值改为ci(以便最后求出总的代价);如果不能实现字典序排序,那就不改变dpi的值,最后结束时只要判断dpi是否为初始设置的极端值,就能判断所有的字符串是否能按字典序排序。

  借助的dp数组,要巧妙的和需要求的值联系起来,既能作判断,又能求结果。

猜你喜欢

转载自www.cnblogs.com/0424lrn/p/12210881.html
今日推荐