题目链接:

  http://codeforces.com/contest/706/problem/C

题目大意

  n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci(<=109),求所有字符串从上到下符合字典序从小到大的最小费用。无解输出-1。

题目思路:

  【动态规划】

  每个字符串有2种状态,翻转或者不翻转,每次只与上一个字符串是否翻转有关,可以用DP。

  费用很大,要用long long。

  无解的时候我直接break了WA了好久。

 //
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL c[N];
LL f[N][];
int l[];
char s[][N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
mem(f,0x7f);mark=;
for(i=;i<=n;i++)
scanf("%I64d",&c[i]);
scanf("%s",s[]);
l[]=strlen(s[]);
for(i=;i<l[];i++)s[][i]=s[][l[]--i];
f[][]=;f[][]=c[];
for(i=,j=;i<=n;i++,j^=)
{
scanf("%s",s[j]);
l[j]=strlen(s[j]);
for(k=;k<l[j];k++)s[j+][k]=s[j][l[j]--k];s[j+][k]='\000';
if(strcmp(s[j],s[j^])>=)
f[i][]=min(f[i-][],f[i][]);
if(strcmp(s[j],s[(j^)+])>=)
f[i][]=min(f[i-][],f[i][]);
if(strcmp(s[j+],s[j^])>=)
f[i][]=min(f[i-][]+c[i],f[i][]);
if(strcmp(s[j+],s[(j^)+])>=)
f[i][]=min(f[i-][]+c[i],f[i][]);
}
if(f[n][]==f[][] && f[n][]==f[][])puts("-1");
else printf("%I64d\n",min(f[n][],f[n][]));
}
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 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 ...

  3. CodeForces 706C Hard problem (水DP)

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

  4. CodeForces 706C Hard problem

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

  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. [javascirpt] Regex

    To Currency function toCurrency(price){ return price.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, & ...

  2. pythonQQ邮件

    http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html http://blog.csdn.net/imzoer/art ...

  3. Qt 学习之路 :Qt 绘制系统简介

    Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...

  4. C++初始化顺序

    1. 全局和类的静态变量成员在main之前构造和初始化,静态成员不能在类的内部构造初始化 2. 类的普通成员依据在类内的定义顺序初始化,类的构造函数的初始化类表只能决定成员的构造函数,不能决定构造顺序 ...

  5. android中获取时间

    android中获取时间 1)通过calendar类获取 Calendar calendar = Calendar.getInstance();int moth = calendar.get(Cale ...

  6. Tips--怎么使用谷歌搜索

    修改hosts即可: hosts在哪? windows下:C:\Windows\System32\drivers\etc 管理员身份打开,并将下载好的hosts文件内容,添加到原有的hosts文件末尾 ...

  7. HUD2087

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using name ...

  8. redisbook笔记——redis内存映射数据结构

    虽然内部数据结构非常强大,但是创建一系列完整的数据结构本身也是一件相当耗费内存的工作,当一个对象包含的元素数量并不多,或者元素本身的体积并不大时,使用代价高昂的内部数据结构并不是最好的办法. 为了解决 ...

  9. System.Management命名空间

    提供对大量管理信息和管理事件集合的访问,这些信息和事件是与根据 Windows 管理规范 (WMI) 结构对系统.设备和应用程序设置检测点有关的.应用程序和服务可以使用从 ManagementObje ...

  10. [技术翻译]Guava-libraries(一): 用户指导

    用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...