CodeForces 706C Hard problem

举报
用户已注销 发表于 2021/11/19 05:06:01 2021/11/19
【摘要】 题目:   Description 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 g...

题目:

 

Description

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

Sample Input

Input

        
  1. 2
  2. 1 2
  3. ba
  4. ac
Output
1

       
Input

        
  1. 3
  2. 1 3 1
  3. aa
  4. ba
  5. ac
Output
1

       
Input

        
  1. 2
  2. 5 5
  3. bbb
  4. aaa
Output
-1

       
Input

        
  1. 2
  2. 3 3
  3. aaa
  4. aa
Output
-1
       

 

 

 

代码:


  
  1. #include<iostream>
  2. #include<string>
  3. #include<cstring>
  4. using namespace std;
  5. int c[100000];
  6. bool ok(string s1,int k1, string s2,int k2)
  7. {
  8. string s3;
  9. int l1 = s1.length(), l2 = s2.length();
  10. if (k1)
  11. {
  12. s3 = s1;
  13. for (int i = 0; i < l1; i++)s1[i] = s3[l1 - i - 1];
  14. }
  15. if (k2)
  16. {
  17. s3 = s2;
  18. for (int i = 0; i < l2; i++)s2[i] = s3[l2 - i - 1];
  19. }
  20. int i = 0;
  21. while (i < l1 && i < l2)
  22. {
  23. if (s1[i] < s2[i])return true;
  24. if (s1[i] > s2[i])return false;
  25. i++;
  26. }
  27. if (l1 <= l2)return true;
  28. return false;
  29. }
  30. int main()
  31. {
  32. int n;
  33. long long sum1 = 0, sum2 = 0, t1, t2;
  34. scanf("%d", &n);
  35. for (int i = 0; i < n; i++)scanf("%d", &c[i]);
  36. string s1="", s2;
  37. for (int i = 0; i < n; i++)
  38. {
  39. cin >> s2;
  40. t1 = -1, t2 = -1;
  41. if (sum1 >= 0)
  42. {
  43. if (ok(&s1[0], 0, &s2[0], 0))t1 = sum1;
  44. if (ok(&s1[0], 0, &s2[0], 1))t2 = sum1 + c[i];
  45. }
  46. if (sum2 >= 0)
  47. {
  48. if (ok(&s1[0], 1, &s2[0], 0))
  49. if (t1<0 || t1>sum2)t1 = sum2;
  50. if (ok(&s1[0], 1, &s2[0], 1))
  51. if (t2<0 || t2>sum2 + c[i])t2 = sum2 + c[i];
  52. }
  53. sum1 = t1;
  54. sum2 = t2;
  55. s1 = s2;
  56. }
  57. if (sum1 < 0 && sum2 < 0)cout << -1;
  58. else if (sum1 < 0)cout << sum2;
  59. else if (sum2 < 0)cout << sum1;
  60. else cout << ((sum1 < sum2) ? sum1 : sum2);
  61. cout << endl;
  62. return 0;
  63. }

 

用了空间压缩,把一维数组压成了sum1、sum2、t1、t2这4个变量。

所以看起来动态规划的特征不是很明显,不过仔细想想,本质上还是动态规划。

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/52199737

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。