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条字符串为字典序排序(短的在前,小的在前)

 #include<bits/stdc++.h>
using namespace std; #define LL long long
const int maxn = ;
const LL INF=1e15; int c[maxn];
string str[maxn][];
LL dp[maxn][]; string reverse(string s){//翻转
string res=s;
int i, len=res.length();
for(i=;i<len/;++i)
swap(res[i], res[len--i]);
return res;
} int main(){
int n, i;
scanf("%d",&n);
for(i=;i<n;++i)
scanf("%d",&c[i]); for(i=;i<n;++i){
cin>>str[i][];
str[i][]=reverse(str[i][]);//翻转
} for(i=;i<n;++i)//初始化
dp[i][]=dp[i][]=INF;
dp[][]=; dp[][]=c[]; for(i=;i<n;++i){
if(str[i][]>=str[i-][])//原str i 大于 原str i-1
dp[i][]=dp[i-][];
if(str[i][]>=str[i-][])//翻转str i 大于 原str i-1
dp[i][]=dp[i-][]+c[i];
if(str[i][]>=str[i-][])//原str i 大于 翻转str i-1
dp[i][]=min(dp[i][],dp[i-][]);
if(str[i][]>=str[i-][])//翻转str i 大于 翻转str i-1
dp[i][]=min(dp[i][],dp[i-][]+c[i]);
if(dp[i][]==INF&&dp[i][]==INF)
break;
}
if(i==n)
printf("%I64d\n",min(dp[n-][],dp[n-][]));
else
printf("-1\n"); return ;
}

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

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

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

dp--B - Hard problem的更多相关文章

  1. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  2. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  3. BZOJ1003 物流运输 最短路+DP

    1003: [ZJOI2006]物流运输 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条 ...

  4. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  5. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. DP&图论 DAY 3 下午 考试

    Problem AProblem Description有一天 Tarzan 写了一篇文章,我们发现这文章当中一共出现了 n 个汉字,其中第 i 个汉字出现了 ai 次,因为 Tarzan 不希望文章 ...

  7. [GodLove]Wine93 Tarining Round #8

    比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...

  8. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

  9. BZOJ 2466: [中山市选2009]树

    Sol 树形DP. 听说有非常神奇的高斯消元的做法...orz... 然而我只会 \(O(n)\) 的树形DP. 首先一个点的状态只于他的父节点和子树有关,跟他 子树的子树 和 父亲的父亲 都没有任何 ...

  10. [GodLove]Wine93 Tarining Round #3

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44857#overview 题目来源: ZOJ Monthly, July 2 ...

随机推荐

  1. 小程序tabbar和navigator一起使用点不动

    在项目开发中我遇到这样的一个问题,页面需要navigator链接跳转一个页面,tabbar也需要导航到这个页面,最开始还没有添加tabbar的时候,navigator都能够正常跳转,但是当加上tabb ...

  2. node-express处理表单的接口

    写一个小接口,用postman测试接口是否可行

  3. Android布局样式

    本篇介绍一下Android中的几种常用的布局,主要介绍内容有: ·View视图 ·LinearLayout ·RelativeLayout 在介绍布局之前,我们首先要了解视图View的基本属性,因为所 ...

  4. [[FJOI2016]神秘数][主席树]

    明白之后 5min 就写好了-自闭- 这题的题意是问你 \([L,R]\) 区间的数字不能构成的数字的最小值- 首先考虑 如果 \([1,x]\) 可以被表示 那么加入一个 \(a_i\) 显然 \( ...

  5. 正则表达式[\w]+,\w+,[\w+]

    正则表达式[\w]+,\w+,[\w+] 三者区别? [],[ABC]+,[\w./-]+ 表达什么? 正则表达式[\w]+,\w+,[\w+] 三者有何区别:[\w]+和\w+没有区别,都是匹配数字 ...

  6. Codeforces Round #622 (Div. 2) C2 - Skyscrapers (hard version) 单调栈

    从左往右扫,找到比第i个小的第一个数字,l[i] = l[last] + (i - last) * m[i],用单调栈O(n)维护这个过程,再从右往左扫,同理可以算出r数组,注意一下long long ...

  7. python的优先级

    在编写程序时,我遇到麻烦!怎么找都找不到bug 最终我发现了是我搞错了运算符优先级 位运算要在加减后面(这可真奇怪) eg 10-10^11=11!!! 还是多加括号的好

  8. C#依赖注入 简体demo

      class Program { static void Main(string[] args) { Dal dal = new MySql(); dal.Add(); Dal dal1 = new ...

  9. Spark学习之路 (三)Spark之RDD[转]

    RDD的概述 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...

  10. 【Unity|C#】基础篇(18)——正则表达式(Regex类)

    [学习资料] <C#图解教程>:https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.com/s/ ...