cf 706C Hard Problem(dp)
来源:互联网 发布:2014网络流行语 编辑:程序博客网 时间:2024/06/07 01:43
>
C. Hard problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard outputVasiliy 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.
InputThe 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.
OutputIf 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
Input2
1 2
ba
acOutput
1
Input
3
1 3 1
aa
ba
acOutput
1
Input
2
5 5
bbb
aaaOutput
-1
Input
2
3 3
aaa
aaOutput
-1
Note
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.
更之前做的那个poj上的一道题很是类似,直接用双数组来维护一下就好。
代码
*===============================================================* Copyright (C) 2016 All rights reserved.* * Filename: C.cpp* Author: gsh* Created_time:2016/8/14 星期日 15:04:36* Problem_tags:* Problem_keyword:================================================================*/#include <bits/stdc++.h>#define LOCALtypedef long long ll;typedef unsigned long long ull;using namespace std;const ll INF = 1000000000000000;const int maxn = 1e5 + 100;ll dp[maxn][2], c[maxn];string reverse(string s){ string t = ""; for(int i = s.size()-1; i >= 0; i--) t += s[i]; return t;}int main(){#ifdef LOCAL //freopen("C.in","r",stdin); //freopen("C.out","w",stdout);#endif int n; scanf("%d", &n); for(int i = 0; i < n; i++) cin >> c[i]; string last; cin >> last; dp[0][0] = 0; dp[0][1] = c[0]; for(int i = 1; i < n; i++) { string now; cin >> now; dp[i][0] = dp[i][1] = INF; if(last <= now) dp[i][0] = min(dp[i][0], dp[i-1][0]); if(reverse(last) <= now) dp[i][0] = min(dp[i][0], dp[i-1][1]); if(last <= reverse(now)) dp[i][1] = min(dp[i][1], dp[i-1][0] + c[i]); if(reverse(last) <= reverse(now)) dp[i][1] = min(dp[i][1], dp[i-1][1] + c[i]); last = now; } ll ans = min(dp[n-1][0], dp[n-1][1]); //cout << ans << endl; if(ans >= INF) cout << -1 << endl; else cout << ans << endl; return 0;}
- cf 706C Hard Problem(dp)
- CF#367(Div2)C. Hard problem (DP)
- Codeforces 706C Hard problem(dp+字符串)
- Codeforces 706C Hard problem (DP)
- Codeforces 706C Hard problem (DP)
- CodeForces 706C Hard problem【dp】
- CodeForces 706 C. Hard problem(dp)
- 706C. Hard problem
- codefoces 706C Hard problem (dp/dfs)
- CodeForces 706C-Hard problem(基本的DP)
- CodeForces 367C Hard problem DP
- Codeforces-706C Hard problem
- CodeForces 706C Hard problem
- 【Codeforces 706C . Hard problem】
- codeforces 706C Hard problem
- Codeforces 706C Hard problem
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- 【Codeforces Round 367 (Div 2) C】【简单DP】Hard problem
- VB键盘钩子源码:截取一切键盘按键
- android listview中checkbox(购物车,批量操作)
- android 使用xposed安装器
- bzoj2820(莫比乌斯反演)
- Spring-boot-admin功能说明
- cf 706C Hard Problem(dp)
- python实现爬虫--以CSDN为例
- Android的基本控件(2)
- HDU-1599-find the mincost route【最短路】【无向图最小环】
- 影响数据库性能的因素
- 文件I/O
- Struts2框架13.值栈
- CH3-类与对象
- Search in Rotated Sorted Array II