zoukankan      html  css  js  c++  java
  • codeforces 1183H

    题目链接:https://codeforces.com/contest/1183/problem/H

    Problem:

    The only difference between the easy and the hard versions is constraints.

    A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols.

    Characters to be deleted are not required to go successively, there can be any gaps between them.

    For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string).

    But the following strings are not subsequences: "aabaca", "cb" and "bcaa".

    You are given a string consisting of lowercase Latin letters.

    In one move you can take any subsequence of the given string and add it to the set S. The set S can't contain duplicates.

    This move costs n|t|, where |tis the length of the added subsequence (i.e. the price equals to the number of the deleted characters).

    Your task is to find out the minimum possible total cost to obtain a set of size or report that it is impossible to do so.

    Input

    The first line of the input contains two integers and k (1n100,1k1012) — the length of the string and the size of the set, correspondingly.

    The second line of the input contains a string consisting of lowercase Latin letters.

    Output

    Print one integer — if it is impossible to obtain the set S

    of size k

    , print -1. Otherwise, print the minimum possible total cost to do it.

    Examples

    Input
    4 5
    asdf
    
    Output
    4
    
    Input
    5 6
    aaaaa
    
    Output
    15
    
    Input
    5 7
    aaaaa
    
    Output
    -1
    
    Input
    10 100
    ajihiushda
    
    Output
    233

    Note

    In the first example we can generate S= { "asdf", "asd", "adf", "asf", "sdf" }.

    The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.

    题目大意:给一个长度为n的字符串,有没有至少k个不同的子序列,有的话输出最少的花费。每个子序列的花费为原串长度减去子序列长度。

    思路:并不是一道复杂的题,首先如果不考虑重复的话就是 2^n 种情况,就要考虑搜索去重,dp[i][j]表示前i个字符中长度为j的子序列的数量。

    ac代码:

    #include <cstdio>
    #include <cstring>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <cstring>
    #include <stack>
    #define MAX_N 110
    using namespace std;
    typedef long long ll;
    
    int n;
    ll k,ans;
    ll dp[MAX_N][MAX_N];
    int pre[MAX_N],last[MAX_N];
    char s[MAX_N];
    
    int main()
    {
    	scanf("%d%lld",&n,&k);
    	scanf("%s",s+1);
    	for(int i=1;i<=n;i++){
    		int x=s[i]-'a';
    		if(last[x])	pre[i]-last[x];
    		last[x]=i;
    	}
    	for(int i=0;i<=n;i++)	dp[i][0]=1;
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			dp[i][j]=dp[i-1][j-1]+dp[i][j-1];
    			if(pre[i])	dp[i][j]-=dp[pre[i]-1][j];
    		}
    	}
    	for(int i=n;i>=0;i--){
    		ll cnt=min(k,dp[n][i]);
    		ans+=cnt*(n-i);
    		k-=cnt;
    		if(k<=0)	break;
    	}
    	if(k>0)	ans=-1;
    	printf("%lld
    ",ans);
    
    	return 0;
    }
    

      

  • 相关阅读:
    try-catch- finally块中, finally块唯一不执行的情况是什么?
    Mac下 pygame.image.load不显示图片解决方法
    Ubuntu中sudo速度慢的解决方法(转载)
    [Mac Terminal] ___中断跳出死循环
    Fuzzy logic
    Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.4 Predicates and Quantifiers
    Bugs -----The largest and most interesting kind of thing.
    A Brief, Incomplete, and Mostly Wrong History of Programming Languages
    MIPS指令集
    >> 计算机的数据表示
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12734545.html
Copyright © 2011-2022 走看看