Hard problem CodeForces
来源:互联网 发布:金字塔软件官网 编辑:程序博客网 时间:2024/06/07 07:33
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 exceed 100 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.
21 2baac
1
31 3 1aabaac
1
25 5bbbaaa
-1
23 3aaaaa
-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.
题目意思很好都懂,可以看得出来是一个比较有意思的dp。难点在于怎么表达状态。
dp[i][1] 表示前i个字符串在第i个字符串未反转的前提下的最小消耗;
dp[i][2]表示前i个字符串在第i个字符串反转的前提下的最小消耗。
这样我们就很容易找到状态的转移方程。
详见代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<string>using namespace std;long long dp[100002][3];long long val[100002];string wei[100002];string fan[100002];int main(){ int n; //memset(dp, 0, sizeof(dp)); scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%lld", &val[i]); } for(int i=1; i<=n; i++) { cin >> fan[i]; wei[i] = fan[i]; reverse(fan[i].begin(),fan[i].end()); } // 1 wei // 2 fan dp[1][1] = 0; dp[1][2] = val[1]; int flag = 1; for(int i=2; i<=n; i++) { dp[i][1] = dp[i][2] = 10000000000000000; flag = 0; if(wei[i] >= wei[i-1]) { flag = 1; dp[i][1] = min(dp[i][1], dp[i-1][1]); } if(wei[i] >= fan[i-1]) { flag = 1; dp[i][1] = min(dp[i][1] , dp[i-1][2]); } if(fan[i] >= wei[i-1]) { flag = 1; dp[i][2] = min(dp[i][2], dp[i-1][1]+val[i]); } if(fan[i] >= fan[i-1]) { flag = 1; dp[i][2] = min(dp[i][2], dp[i-1][2]+val[i]); } if(dp[i][2]==10000000000000000 && dp[i][1]==10000000000000000)flag=0; if(flag==0)break; } if(flag==0)printf("-1\n"); else printf("%lld\n", min(dp[n][1], dp[n][2])); return 0;}水波。
- Hard problem CodeForces
- Codeforces-706C Hard problem
- CodeForces 706C Hard problem
- 【Codeforces 706C . Hard problem】
- codeforces 706C Hard problem
- codeforces-367# C. Hard problem
- Codeforces 706C Hard problem
- Codeforces-687A NP-Hard Problem
- Codeforces 687A NP-Hard Problem
- CodeForces 688C-NP-Hard Problem(dfs)
- CodeForces 687A NP-Hard Problem
- CodeForces-688C.NP-Hard Problem
- Codeforces 706C Hard problem(dp+字符串)
- Codeforces Round #367 C. Hard problem
- CodeForces 367C Hard problem DP
- codeforces 706 C. Hard problem #367 div2
- 【动态规划】Codeforces 706C Hard problem
- Codeforces 706C Hard problem (DP)
- HDU
- MemberCache学习
- LeetCode系列之Two sum
- spring boot1.5.X以上版本@ConfigurationProperties取消location注解后的替代方案
- HibernateDaoSupport和HibernateTemplate
- Hard problem CodeForces
- Mysql 中有关日期的函数(sql)
- Leetcode题解
- HoughLinesP( )函数详解
- Linux常用命令
- Zookeeper原子广播Zab
- 1008. 数组元素循环右移问题
- ZooKeeper集群
- 夜雨,心烦