链接[http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/C]

题意:

他希望它们按词典顺序排序(就像字典中那样),但他不允许交换其中的任何一个。

唯一允许他做的操作是将其中的任何一个反转(第一个字符变成最后一个,

第二个字符变成最后一个,以此类推)。

思路;

DP,不断更新花费的值,并且判断是否可以满足字典序排序。

dp[i][0]表示,表示排到第i+1个字符串,不需要反转需要的花费,dp[i][0]表示排到第i+1个字符串,需要反转需要的花费。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
string reverse(string s)
{
string res=s;
int i,len=res.length();
for(i=0;i<len/2;++i)
swap(res[i],res[len-1-i]);
return res;
}
string s[100005][2];//二维字符串数组
ll dp[100005][2];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0); //map<string,int> m;
int n,i;
ll a[100005];
//freopen("in.txt","r",stdin);
while(cin>>n){
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;++i)
dp[i][0]=dp[i][1]=9999999999999999;
dp[0][0]=0,dp[0][1]=a[0];
for(i=0;i<n;++i)
{
cin>>s[i][0];
s[i][1]=reverse(s[i][0]);
}
for(i=1;i<n;i++)
{
if(s[i][0]>=s[i-1][0])
dp[i][0]=dp[i-1][0];//二者都不需要反转
if(s[i][1]>=s[i-1][0])
dp[i][1]=dp[i-1][0]+a[i];//后者需要反转就dp[i-1][0]+a[i]
if(s[i][0]>=s[i-1][1])
dp[i][0]=min(dp[i][0],dp[i-1][1]);//前者需要反转,因为开始初始化一个很大的数所以要比较
if(s[i][1]>=s[i-1][1])
dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);//二者都需要反转,且初始化为很大,需要比较
if(dp[i][0]==9999999999999999&&dp[i][1]==9999999999999999)
//如果不满足字典序排序,就退出DP
break;
}
ll ans=min(dp[n-1][0],dp[n-1][1]);
if(i>=n) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}

CF367C. Hard problem的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

随机推荐

  1. [Demo_03] MapReduce 实现多类型输出

    0. 说明 MapReduce 实现将最高气温统计数据输出为文本格式和 SequenceFile 格式 在最高气温统计的基础上进行操作 1. 核心代码 // 多输出格式设置 MultipleOutpu ...

  2. Burp Suite 抓取http、https流量配置+CA证书安装

    HTTPS协议是为了数据传输安全的需要,在HTTP原有的基础上,加入了安全套接字层SSL协议,通过CA证书来验证服务器的身份,并对通信消息进行加密.基于HTTPS协议这些特性,我们在使用Burp Pr ...

  3. Linux 小知识翻译 - 「Linux」和病毒

    据说,「Linux」系统上的病毒要远远少于Windows系统上病毒.从2种系统的普及度来看,这是很显然的, 「Linux」的使用人群很少,所以「Linux」上的病毒的扩散时,受害的范围也不大. 但是, ...

  4. Lua 与 C 交互值 函数调用(2)

    @(语言) Lua和C 函数间的使用,都是通过栈来交互,并且基于遵守一定的规则,按照这个规则来就可以了. 1. 调用Lua函数 调用Lua方法过程 将被调用的函数入栈: 依次将所有参数入栈: 使用 l ...

  5. Orcale新增、修改、删除字段

    一.新增字段 alert table user add( userName VARCHAR2(255 CHAR) ) ; 设置字段不为空, 给出默认值 alert table user add( us ...

  6. 类的反射_reflex

    JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为j ...

  7. CRM项目之stark组件(2)

    那么从今天开始呢,我们就要开始设计属于我们自己的admin组件,起个名字就叫stark吧(当然你愿意叫什么都可以). stark组件之四步走 仿照admin组件实现流程,stark组件要实现四件事情: ...

  8. 如何快速安装visual studio 2017和破解

    https://sm.myapp.com/original/Development/vs_community__1229872941.1512460494-v15.5.0.exe visual stu ...

  9. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

  10. 最简单例子图解JVM内存分配和回收(转)

    本文转自http://ifeve.com/a-simple-example-demo-jvm-allocation-and-gc/ http://www.idouba.net/a-simple-exa ...