简单$dp$。

$dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用。

$dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前$i$个串字典序呈非递减的状态下的最小费用。

那么可以得到以下递推式:

如果$s[i] > s[i - 1]$,$dp\left[ i \right]\left[ 0 \right] = min(dp\left[ i \right]\left[ 0 \right],dp\left[ {i - 1} \right][0])$;

如果$s[i] > s{[i - 1]_{reverse}}$,$dp\left[ i \right]\left[ 0 \right] = min(dp\left[ i \right]\left[ 0 \right],dp\left[ {i - 1} \right][1])$;

如果$s{[i]_{reverse}} > s[i - 1]$,$dp\left[ i \right]\left[ {1\left] { = min(dp} \right[i} \right]\left[ {1\left] {,dp} \right[i - 1} \right]\left[ 0 \right] + c\left[ i \right])$;

如果$s{[i]_{reverse}} > s{[i - 1]_{reverse}}$,$dp\left[ i \right]\left[ {1\left] { = min(dp} \right[i} \right]\left[ {1\left] {,dp} \right[i - 1} \right]\left[ 1 \right] + c\left[ i \right])$;

初始化的时候,令$dp[i][j]=INF$。如果$dp[n][0]$和$dp[n][1]$都是$INF$,那么输出$-1$,否则输出$min(dp[n][0],dp[n][1])$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const LL INF=1e17;
const int maxn=;
char s[maxn],t[maxn];
LL dp[maxn][],c[maxn];
int n; void res()
{
int len=strlen(s);
for(int i=;i<len/;i++) swap(s[i],s[len-i-]);
} void ret()
{
int len=strlen(t);
for(int i=;i<len/;i++) swap(t[i],t[len-i-]);
} int main()
{
scanf("%d",&n); memset(s,,sizeof s); memset(t,,sizeof t);
for(int i=;i<=n;i++) scanf("%lld",&c[i]);
for(int i=;i<=n;i++) dp[i][]=dp[i][]=INF;
scanf("%s",s); dp[][]=; dp[][]=c[];
for(int i=;i<=n;i++)
{
strcpy(t,s); scanf("%s",s); if(strcmp(s,t)>=) dp[i][]=min(dp[i][],dp[i-][]);
ret(); if(strcmp(s,t)>=) dp[i][]=min(dp[i][],dp[i-][]); ret(); res(); if(strcmp(s,t)>=) dp[i][]=min(dp[i][],dp[i-][]+c[i]);
ret(); if(strcmp(s,t)>=) dp[i][]=min(dp[i][],dp[i-][]+c[i]); res(); ret();
}
LL ans=min(dp[n][],dp[n][]);
if(ans==INF) printf("-1\n");
else printf("%lld\n",ans);
return ;
}

CodeForces 706C Hard problem的更多相关文章

  1. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  2. 【动态规划】Codeforces 706C Hard problem

    题目链接: http://codeforces.com/contest/706/problem/C 题目大意: n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci( ...

  3. Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏

    C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. CodeForces 706C Hard problem (水DP)

    题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...

  5. CodeForces - 706C Hard problem(dp+字符串)

    题意:有n个字符串,只能将其逆转,不能交换位置,且已知逆转某字符串需要消耗的能量,问将这n个字符串按字典序从小到大排序所需消耗的最少能量. 分析:每个字符串要么逆转,要么不逆转,相邻两个字符串进行比较 ...

  6. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  7. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  8. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  9. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

随机推荐

  1. Jackson 格式化日期问题

    Jackson 默认是转成timestamps形式的,如何使用自己需要的类型, 解决办法: 1.在实体字段上使用@JsonFormat注解格式化日期 @JsonFormat(locale=" ...

  2. [转]Even when one byte matters

    Source:http://kernelbof.blogspot.jp/2009/07/even-when-one-byte-matters.html Common Vulnerabilities a ...

  3. [转]Avoiding GDB Signal Noise.

    原文:http://peeterjoot.wordpress.com/2010/07/07/avoiding-gdb-signal-noise/ A quick note for future ref ...

  4. 用django搭建一个简易blog系统(翻译)(一)

    Django 入门 原始网址: http://www.creativebloq.com/netmag/get-started-django-7132932 代码:https://github.com/ ...

  5. PHP实现一个简单url路由功能

    如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染.很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的 ...

  6. SQLSERVER之高灵活的业务单据流水号生成

    SQLSERVER之高灵活的业务单据流水号生成 最近的工作中要用到流水号,而且业务单据流水号生成的规则分好几种,并非以前那种千篇一律的前缀+日期+流水号的简单形式,经过对业务的分析,以及参考网上程序员 ...

  7. 记录下自己写的gulp打包脚本

    var c = { rootPath: 'src',//项目文件夹 outputPath: 'output',//文件编译后的输出目录 revPath: 'manifest',//rev映射文件目录 ...

  8. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

  9. 浅谈XSS

    最近在做项目中的漏洞修复工作,在短时间内接触到很多关于web开发需要防范的漏洞,例如SQL injection , XSS, CSRF等等,这些漏洞对web开发的项目来说的破坏还是比较大的,其实在网上 ...

  10. 通过jquery来实现文本框和下拉框动态添加效果,能根据自己的需求来自定义最多允许添加数量,实用的jquery动态添加文本框特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...