2019 Multi-University Training Contest 1 I String

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2642    Accepted Submission(s): 707


 

Problem Description

Tom has a string containing only lowercase letters. He wants to choose a subsequence of the string whose length is k and lexicographical order is the smallest. It's simple and he solved it with ease.
But Jerry, who likes to play with Tom, tells him that if he is able to find a lexicographically smallest subsequence satisfying following 26 constraints, he will not cause Tom trouble any more.
The constraints are: the number of occurrences of the ith letter from a to z (indexed from 1 to 26) must in [Li,Ri].
Tom gets dizzy, so he asks you for help.

 

 

Input

The input contains multiple test cases. Process until the end of file.
Each test case starts with a single line containing a string S(|S|≤105)and an integer k(1≤k≤|S|).
Then 26 lines follow, each line two numbers Li,Ri(0≤Li≤Ri≤|S|). 
It's guaranteed that S consists of only lowercase letters, and ∑|S|≤3×105.

 

 

Output

Output the answer string.
If it doesn't exist, output −1.

 

 

Sample Input

 

aaabbb 3 0 3 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 

 

Sample Output

 

abb

 

 

Source

2019 Multi-University Training Contest 1

 

题目大意:给了一个字符串,和26个限制区间,表示a-z的出现次数必须在这个区间里,要求输出长度为k并且字典序最小的字符串。

解题思路:容易想到是个贪心,但是check函数比较难想到。 我们依次尝试a-z,对于当前尝试的字符怎么才能知道

如果取了当前字符,后边的字符还能不能满足限制条件。

可以用一个ues数组记录每个字符用了多少个。

当前枚举的字符能够被选择必须满足3个条件。

1。在当前的位置后边,已用的字符+剩下的字符都大于等于下限。

2。已经确定的长度加上满足下限至少要取的长度必须小于等于k

3。如果每一个字符都取上限还凑不够k个的话也是不行的。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5;
char s[N];
int pre[30];
int nt[N][30],sum[N][30];
char s1[N];
int L[N],R[N];
int ues[30];

//1.如果后边有字幕的个数小于下限则不行
//2.判断每个字符最少还需要取几个,最多还有几个
bool judge(int del,int k,int pos,int n)
{
    int mini=0,mmax=0;
    for(int i=0;i<26;i++)
    {
        if(ues[i]+sum[n][i]-sum[pos][i]<L[i]) return false;
        mini+=max(L[i]-ues[i],0);
        mmax+=min(R[i]-ues[i],sum[n][i]-sum[pos][i]);
    }
    if(del+mini<=k && del+mmax>=k) return true;
    return false;
}

int top;
bool solve(int n,int k)
{
    int flag;
    top=0;
    for(int i=1;i<=n; )
    {
        flag=0;
        int pos;
        for(int j=0;j<26;j++)
        {
            if(ues[j]==R[j])continue;
            ues[j]++;
            //cout<<"judge "<<i<<" "<<j<<endl;
            if(judge(top+1,k,nt[i][j],n))
            {
                s1[++top]='a'+j;
                if(top==k)return true;
                flag=1;
                pos=j;
                break;
            }
            ues[j]--;
        }
        if(!flag)return false;
        i=nt[i][pos]+1;
    }
    return top==k;
}

int main()
{
    //freopen("D:ou.txt","w",stdout);
    while(scanf("%s",s+1)!=EOF){
    memset(ues,0,sizeof(ues));
    int k;scanf("%d",&k);
    for(int i=0;i<26;i++)scanf("%d%d",&L[i],&R[i]);
    int len=strlen(s+1);
    for(int i=0;i<26;i++)pre[i]=len+1;

    for(int i=len;i>=1;i--)
    {
        int id=s[i]-'a';
        pre[id]=i;
        for(int j=0;j<26;j++)nt[i][j]=pre[j];
    }


    for(int i=1;i<=len;i++)
    {
        int id=s[i]-'a';
        for(int j=0;j<26;j++)
        {
            if(j==id)sum[i][j]=sum[i-1][j]+1;
            else sum[i][j]=sum[i-1][j];
        }
    }

    if(!solve(len,k))puts("-1");
    else{
        for(int i=1;i<=top;i++)putchar(s1[i]);
        printf("\n");
    }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值