codeforces 706C C. Hard problem(dp)
题目链接:
1 second
256 megabytes
standard input
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.
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.
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.
2
1 2
ba
ac
1
3
1 3 1
aa
ba
ac
1
2
5 5
bbb
aaa
-1
2
3 3
aaa
aa
-1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <bits/stdc++.h>
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<class T> 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<class T> 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<<ans;
return 0;
}
最新文章
- CSS浮动、定位
- linuxqq
- 有用的MySQL语句
- 最大后验估计(MAP)
- 汇总:Linux下svn命令大全
- CSS3 :nth-child() 选择器
- SimpleDateFormat线程不安全问题处理
- UML01-概述
- [SignalR]异常信息捕获以及处理
- 7-days-asp-dotnet-mvc-day1
- Tyvj P3276
- STM32驱动DHT11温湿度传感器
- Codeforces 802L Send the Fool Further! (hard)
- 深入理解springAOP切面的特性
- css块居中
- 使用electron开发指静脉客户端遇到的问题总结
- CAS (15) — CAS 线上环境 Ehcache Replication 的非稳定重现错误 java.util.ConcurrentModificationException
- Locust 分布式测试
- Android ViewPager设置监听注意事项
- 13 Calculating Expected Offspring
热门文章
- SQL EXISTS 与 IN
- Deploying Docker images via SSH
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结
- Hibernate学习一----------Hibernate初实现
- eclipse adt开发android ndk没有NDK选项问题的解决方案
- 迁移EXT4
- WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where applicable(附编译脚本)
- HDFS源码分析心跳汇报之数据结构初始化
- WebStorm 常用功能
- JavaScript -- JavaScript高级程序设计