链接[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. Windows Server 2008 R2终端服务器激活方法

    本文描述了如何激活Windows Server 2008 R2的终端服务器的方法. 目录: 1.Windows Server  2008 R2终端服务器的安装 2.Windows Server  20 ...

  2. Django框架的使用教程--mysql数据库[三]

    Django的数据库 1.在Django_test下的view.py里面model定义模型 from django.db import models # Create your models here ...

  3. EasyUI tabs指定要显示的tab

    <div id="DivBox"  class="easyui-tabs" style="width: 100%; height: 100%;& ...

  4. jeDate 日期控件

    写在前面的话: 最近在做一个日期范围的功能,研究了一个12306网站的日期范围选择,他用的是jcalendar.js,没有直接在日历插件里面做判断开始时间小于结束时间 而是自己在代码里面做了判断如下: ...

  5. ajax 数据类型结构

  6. EBS-新增和更新价目表行

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/gh320/article/details/36666133  新增和更新价目表行 --目的:在已 ...

  7. https://leetcode.com/problems/palindromic-substrings/description/

    https://www.cnblogs.com/grandyang/p/7404777.html 博客中写的<=2,实际上<=1也是可以的 相当于判断一个大指针内所有子字符串是否可能为回文 ...

  8. Matlab数据处理——数据的保存和读取方法操作

    1:dlmwrite()函数保存成txt文件 使用方法:      dlmwrite('filename', M)      使用默认分隔符“,”将矩阵M写入文本文件filename中:      d ...

  9. MATLAB的两种移位运算

    MATLAB的两种移位运算: 1)circshift矩阵移位 circshift:循环移位数组 语法:B = circshift(A,shiftize) 说明: B  = circshift(A,sh ...

  10. MATLAB常用快捷键命令总结

    1. 在命令窗口(Command Window)中: 1)[↑.↓]——切换到之前.之后运行过的命令,可以重复按多次来达到你想要的命令: 2)[Tab]——自动补全.在command窗口,输入一个命令 ...