博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 706C C. Hard problem(dp)
阅读量:4330 次
发布时间:2019-06-06

本文共 3471 字,大约阅读时间需要 11 分钟。

题目链接:

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.

Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).

To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.

String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.

For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.

The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.

Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.

Output

If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print  - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.

Examples
input
2 1 2 ba ac
output
1
input
3 1 3 1 aa ba ac
output
1
input
2 5 5 bbb aaa
output
-1
input
2 3 3 aaa aa
output
-1
题意:
 
问把这些字符串按字典序排好,如果第i个需要倒转,花费为a[i],现在求最小花费;
 
思路:
 
dp[i][j]表示第i个串的状态为j时的最小花费,j==0表示不倒转,j==1表示倒转;转移方程看代码;
 
AC代码:
 
#include 
#include
#include
#include
#include
#include
#include
using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)#define mst(ss,b) memset(ss,b,sizeof(ss));typedef long long LL;template
void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}int stk[70], tp;template
inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n');}const LL mod=998244353;const double PI=acos(-1.0);const LL inf=1e18;const int N=1e5+10;const int maxn=1e3+10;const double eps=1e-4;int n;LL a[N],dp[N][2];string s[N];int main(){ read(n); For(i,1,n)read(a[i]); For(i,0,n)dp[i][0]=dp[i][1]=inf; For(i,1,n) { cin>>s[i]; if(i==1){dp[i][0]=0,dp[i][1]=a[1];continue;} if(s[i]>=s[i-1])dp[i][0]=min(dp[i][0],dp[i-1][0]); string temp=""; int len=s[i-1].length(); for(int j=len-1;j>=0;j--) { temp=temp+s[i-1][j]; } if(s[i]>=temp)dp[i][0]=min(dp[i][0],dp[i-1][1]); string t=""; len=s[i].length(); for(int j=len-1;j>=0;j--)t=t+s[i][j]; if(t>=s[i-1])dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]); if(t>=temp)dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]); } LL ans=min(dp[n][0],dp[n][1]); if(ans==inf)cout<<"-1"; else cout<

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5763977.html

你可能感兴趣的文章
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>