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条字符串为字典序排序(短的在前,小的在前)
#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的更多相关文章
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- BZOJ1003 物流运输 最短路+DP
1003: [ZJOI2006]物流运输 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条 ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- DP&图论 DAY 3 下午 考试
Problem AProblem Description有一天 Tarzan 写了一篇文章,我们发现这文章当中一共出现了 n 个汉字,其中第 i 个汉字出现了 ai 次,因为 Tarzan 不希望文章 ...
- [GodLove]Wine93 Tarining Round #8
比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...
- [GodLove]Wine93 Tarining Round #7
比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...
- BZOJ 2466: [中山市选2009]树
Sol 树形DP. 听说有非常神奇的高斯消元的做法...orz... 然而我只会 \(O(n)\) 的树形DP. 首先一个点的状态只于他的父节点和子树有关,跟他 子树的子树 和 父亲的父亲 都没有任何 ...
- [GodLove]Wine93 Tarining Round #3
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44857#overview 题目来源: ZOJ Monthly, July 2 ...
随机推荐
- Bazinga HDU - 5510【技巧暴力+字符串】
题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...
- 【笔记】机器学习 - 李宏毅 - 7 - Deep Learning
深度学习发展历史: 感知机和逻辑回归很像,只是没有\(sigmoid\)激活函数. 深度学习训练的三个步骤: Step1:神经网络(Neural network) Step2:模型评估(Goodnes ...
- JavaSE学习笔记(6)---异常
JavaSE学习笔记(6)---异常 软件程序在运行过程中,非常可能遇到问题,我们称之为异常,英文是:Exception,意思是例外.遇到这些例外情况,或者叫异常,我们怎么让写的程序做出合理的处理 ...
- 有关es6的模块化
假如有两个文件:app.js和config.js app.js为主文件要去引用config这个模块 以前学习node时使用的模块导出: 需求:导出三个成员,分别是 foo: bar f: functi ...
- gulp常用插件之gulp-inject使用
更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-inject这个插件的作用与wiredep类似,不同的是可以自己任意指定需要插入文件的列表.它同样是利用注释来寻找插入的位置.获取源文件 ...
- win10中安装与配置maven
原文链接:https://www.cnblogs.com/wkrbky/p/6350334.html Maven安装配置(Windows10) 想要安装 Apache Maven 在Windows 系 ...
- 在java中调用mockjs生成模拟数据
一.手写版 在前端有个模拟数据的神器 Mock.js 能生成随机数据,拦截 Ajax 请求,然后我觉得他的这个生成随机数据不错.然后我就到度娘一顿操作,没找到类似的java实现,于是就有了下面的代码: ...
- Python调用百度地图API实现批量经纬度转换为实际省市地点(api调用,json解析,excel读取与写入)
1.获取秘钥 调用百度地图API实现得申请百度账号或者登陆百度账号,然后申请自己的ak秘钥.链接如下:http://lbsyun.baidu.com/apiconsole/key?applicatio ...
- Codeforce 141A - Amusing Joke (sort)
So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests a ...
- python实用30个小技巧
python实用30个小技巧 展开1.原地交换两个数字Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例: In [1]: x,y = 10 ,20 In [2]: ...