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;
    }
    

      

  • 相关阅读:
    微信小程序---自定义三级联动
    jQuery分页插件
    微信小程序点击图片放大
    微信小程序----日期时间选择器(自定义精确到分秒或时段)
    PHP文件上传error的错误类型
    获取url传的参数转变为对象的方法
    mysql忘记密码时,重新修改密码
    表单注册及自定义validate手机验证码验证实例
    登陆jq表单验证及jqcookie记住密码实例
    iframe高度自适应,自适应子页面高度
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12734545.html
Copyright © 2011-2022 走看看