zoukankan      html  css  js  c++  java
  • codeforces 706C C. Hard problem(dp)

    题目链接:

    C. Hard problem

    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 <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('
    ');
    }
    
    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;
    }
    

      

  • 相关阅读:
    JetBrains Rider 在 Mac 环境下将 cs 文件生成 exe
    Unity3d 控制物体移动、旋转、缩放
    Unity3d 脚本使用规则
    Unity3D 脚本模板修改方法
    Intelli IDEA 设置项目编码(Mac)
    搭建LayaBox的生产环境,并helloWorld
    unity Lighting
    流畅度测试
    Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]
    Unity GUI(uGUI)使用心得与性能总结
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5763977.html
Copyright © 2011-2022 走看看