题目链接:

  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. [Angular 2] ng-model and ng-for with Select and Option elements

    You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...

  2. Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200(转)

    反编译时遇到标题中的异常,根据描述,原因是找不到资源文件,最有可能的原因是apk中使用了系统资源. 解决办法如下: 从手机中导出framework-res.apk文件,该文件在/system/fram ...

  3. android ToolBar与DrawerLayout笔记

    通过Android Studio 生成的Nagvition DrawerLayout Activity 自带的布局中的NagvitionView会覆盖ToolBar直接通到statusBar. 但是自 ...

  4. IDL绘制黑体辐射曲线

    普朗克定律是热红外遥感中常常使用的三大定律之一,描述了黑体辐射能量的情况.绝对黑体的辐射光谱对于研究一切物体的辐射规律具有根本的意义.1900年普朗克引进量子概念,将辐射当做不连续的量子发射,成功地从 ...

  5. windows服务安装(System.ComponentModel.Win32Exception:远程过程调用失败)

    “安装”阶段发生异常.System.ComponentModel.Win32Exception:远程过程调用失败 附上提示信息C:\Windows\Microsoft.NET\Framework\v4 ...

  6. HTML基础语句

    一,网页基础结构: 1 <html> 2 <head> 3 <title>我的第一个网页</title> 4 </head> 5 <b ...

  7. ExtJs的事件机制Event(学员总结)

    一.事件的三种绑定方式 1.HTML/DHTML 在标签中直接增加属性触发事件 [javascript] view plaincopy <script type="text/javas ...

  8. Android中使用PullToRefreshListView遇到的问题

    1.布局文件中要是设置visible属性为Gone的时候,注意了,这样会有一个bug,在代码中setVisible的时候设置为VISIBLE是不起作用的..这个应该是自身的一个小bug(本人目前没有找 ...

  9. unexpected nil window in _UIApplicationHandleEventFromQueueEvent...

    unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: <UI ...

  10. HashMap、HashTable学习

    HashMap: HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Serializable接口. HashMap 的实现不是同步的,这意味着它不是线程安 ...