【CodeForces - 706C】Hard problem(dp,字典序)

题干:

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 exceed 100 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

Note

In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.

In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1.

In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.

题目大意:

题意1:给出n个字符串(n<1e5,且字符串总长度<1e5),可以颠倒任意一个字符串但是不能交换字符串的顺序,颠倒每个字符串都有其对应的花费ci。现需要经过一系列操作满足字符串按照字典序排列,如果能的话输出最小花费,不能输出-1。

题意2:现在有 n 个由小写字母组成的字符串。他想要让这些字符串按字典序排列,但是他不能交换任意两个字符串。他唯一能做的事是翻转字符串。翻转第 i 个字符串需要花费 ci 的能量。他想知道将所有字符串排序最少要多少能量。两个相邻的字符串可以相等,不一定要严格递增。如果不可能有序,输出  - 1。否则输出最小所需的能量。

解题报告:

   其实在封装好的string类中,是重载了>=等一系列运算符的,,而且这题不知道每个字符的长度,显然是需要用string的。不难发现,如果两个字符串s,ss满足字典序,s >= ss 就可以了。又因为这题不能交换字符串不难想到可以dp求解、、然后乱搞一下就好了。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 200000 + 5;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll c[MAX];
string a[MAX],b[MAX];
ll dp[MAX][2];
int main() 
{int n,flag = 1;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",c+i);for(int i = 1; i<=n; i++) {cin>>a[i];b[i] = a[i];reverse(b[i].begin(),b[i].end());}memset(dp,0x3f3f,sizeof dp);dp[1][0]=0;dp[1][1]=c[1];for(int i = 2; i<=n; i++) {if(a[i]>=a[i-1]) dp[i][0]=dp[i-1][0];if(b[i]>=a[i-1]) dp[i][1]=dp[i-1][0]+c[i];if(a[i]>=b[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][1]);if(b[i]>=b[i-1]) dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);if(dp[i][1]==INF && dp[i][0]==INF) {flag=0;break;}}if(flag) printf("%lld\n",min(dp[n][0],dp[n][1]));else printf("-1\n");return 0;
}

总结:  然后这题,初始化的时候用INF啊,别用-1标记非法状态,,不然还得分一堆情况,因为你有取min的操作啊!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441283.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cross_compile = arm-linux-,cross compile grpc for arm

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This post will tell you how to cross compile gPRC static lib for ARM.前段时间尝试交叉编译gRPC遇到了不少的麻烦&#xff0c;写篇post记录一下。gRPCPreparat…

【CodeForces - 349B】Color the Fence (贪心,填数)

题干&#xff1a; Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanyas house. Igor thinks that the larger the number is, the more chance to win Tanyas heart he has. Unfortunately, Igor …

linux键盘映射默认,Linux 中的键盘映射

前面提到&#xff0c;X Window 直接处理了键盘的输入输出端口&#xff0c;因此&#xff0c;在 Linux 虚拟控制台下和 X Window 下使用不同的键盘映射方法。在 Lin对于英语来说&#xff0c;键盘上的字母键直接和英语字母表中的字母对应&#xff0c;但是对于非英语的语种来说&…

【POJ - 2301 】Beat the Spread! (简单数学)

题干&#xff1a; Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final s…

Linux x8664汇编,Linux Udis86 反汇编引擎使用

前两篇说了capstone/beaengine,这节一起用一用经典的udis86;github:https://github.com/vmt/udis860x01:udis86相比于前面两个&#xff0c;用起来还是比较简单的&#xff0c;使用文档如下所示&#xff1a;Getting StartedBuilding and Installing udis86----------------------…

【HDU - 2200】Eddy's AC难题(简单组合数学)

题干&#xff1a; Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来&#xff0c;然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较&#xff0c;他想使第一组中的最…

C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)

一、选择题(1&#xff5e;40题每题1分&#xff0c;41&#xff5e;50题每题2分&#xff0c;共60分)1.微型计算机的运算器、控制器及内存储器组合在一起&#xff0c;称之为()(本题分值&#xff1a;1分)A.ALUB.CPUC.MPUD.主机【正确答案】D2.下列存储器中&#xff0c;存取速度最快…

【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)

题干&#xff1a; Jessicas a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The aut…

c语言全局变量SQR,c语言a/=SQR(k+m);是什么意思?

满意答案NightmareJJ2013.03.16采纳率&#xff1a;47% 等级&#xff1a;12已帮助&#xff1a;11233人先看第一个main() // 主函数{printf("%ld\n", fun(3));} //输出fun(3)这个函数的返回值&#xff0c;3就是下一个//函数中的n 值。long fun (int n) //定义fun函数…

【POJ - 1850】Code (组合数学,字符串另类排序)

题干&#xff1a; Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that …

c语言蓝牙接收6,终于搞定了通过两路蓝牙接收数据

一直想做无线传感器&#xff0c;通过蓝牙来接收数据&#xff0c;无奈因为arduino接收串口数据的一些问题&#xff0c;一直搁到现在。因为学校里给学生开了选修课&#xff0c;所以手边有一些nano和mega可以使用&#xff0c;所以就做了用两个nano加上两个蓝牙模块来发射数据&…

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)

题干&#xff1a; Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time hes explaining that (ab) 2a 22abb 2). So you decide to waste your time with d…

编译原理last集c语言,编译原理作业集-第七章.doc

编译原理作业集-第七章第七章 语义分析和中间代码产生本章要点1. 中间语言&#xff0c;各种常见中间语言形式&#xff1b;2. 说明语句赋值语句布尔表达式控制语句的翻译&#xff1b;3. 过程调用的处理&#xff1b;4. 类型检查&#xff1b;本章目标掌握和理解中间语言&#xff0…

【CodeForces - 244A 】Dividing Orange (构造,水题)

题干&#xff1a; One day Ms Swan bought an orange in a shop. The orange consisted of nk segments, numbered with integers from 1 to nk. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided…

兔子吃萝卜的c语言编程,狼追兔子的c语言实现

满意答案16guoyuming2013.03.05采纳率&#xff1a;49% 等级&#xff1a;13已帮助&#xff1a;8005人用单链表实现#include #includetypedef struct node{int cave;struct node * next;}node,*LinkList;void main(){int i0,j,count1; // 初始值为1&#xff1b;LinkList L,p,h…

【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)

题干&#xff1a; Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x  4, and y  7, then numbers 47, 74…

c语言二叉树构造与输出,C语言数据结构树状输出二叉树,谁能给详细的解释一下...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼void TranslevelPrint(Bitree bt){struct node{Bitree vec[MAXLEN]; //存放树结点int layer[MAXLEN]; //结点所在的层int locate[MAXLEN]; //打印结点的位置int front,rear;}q;int i,j,k;int nLocate;j 1;k 0;q.front 0;q.rear …

【CodeForces - 245C 】Game with Coins (思维,贪心)

题干&#xff1a; Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has aicoins. Polycarpus and Vasily move in turns. Polycarpus moves first. Du…

【CodeForces - 246D】Colorful Graph (暴力,图,存边,STL)

题干&#xff1a; Youve got an undirected graph, consisting of n vertices and m edges. We will consider the graphs vertices numbered with integers from 1 to n. Each vertex of the graph has a color. The color of the i-th vertex is an integer ci. Lets consi…

android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...

Android-Skin-Loader更新日志导入到Android Studio&#xff0c;使用gradle构建皮肤包(见7. 皮肤包是什么&#xff1f;如何生成&#xff1f;)(2015-12-02)解决Fragment换肤在某些版本的support-v4包下失效的问题(感谢javake同学)(2015-12-02)对textColor加入selector类型的资源的…