[hdu6586]String

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)

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 26 26 constraints, he will not cause Tom trouble any more.
The constraints are: the number of occurrences of the ith letter from a a a to z z z (indexed from 1 to 26) must in [ L i , R i ] [L_i,R_i] [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 ∣ ≤ 1 0 5 ) S(|S|≤10^5) S(S105)and an integer k ( 1 ≤ k ≤ ∣ S ∣ ) k(1≤k≤|S|) k(1kS).
Then 26 lines follow, each line two numbers L i , R i ( 0 ≤ L i ≤ R i ≤ ∣ S ∣ ) L_i,R_i(0≤L_i≤R_i≤|S|) Li,Ri(0LiRiS).
It’s guaranteed that S consists of only lowercase letters, and ∑ ∣ S ∣ ≤ 3 × 1 0 5 \sum |S|≤3×10^5 S3×105.

Output
Output the answer string.
If it doesn’t exist, output − 1 −1 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

题意:
给一个字符串s和一个数字k,要求生成s的一个长度为k的子序列,再给26条限制L[i],R[i],表示字符(‘a’+i-1)在新字符串中出现次数为L[i]到R[i],并且字典序最小。
题解:
我们考虑从头到尾构建答案。每次加入能加入的最小的字符,能加入的定义是保证加入这个字符之后,其原串中剩余的每种元素的个数足够满足剩余的限制条件,原串中那个位置每种元素的个数和<=k-当前构造到第几位。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[1000004],ans[1000004];
int sp[1000004][26],l,la;
int L[26],R[26],k;
vector<int>vec[26];
bool check(int lg,int p,int now){
    //cout<<p<<" - "<<" "<<L[p]<<" "<<R[p]<<endl;
    if(R[p]<=0)return 0;
    //cout<<" "<<p<<endl;
    int sum=0;
    for(int i=0;i<26;i++){
        if(i!=p&&sp[lg+1][i]<L[i]){
            return 0;
        }
        sum+=L[i];
    }
    if(L[p]>0)sum--;
    if(sum>k-now)return 0;
    return 1;
}
int w33ha(){
    scanf("%d",&k);
    l=strlen(s+1);
    la=0;
    memset(sp,0,sizeof(sp));
    for(int i=l;i>=1;i--){
        for(int j=0;j<26;j++)sp[i][j]=sp[i+1][j];
        sp[i][s[i]-'a']++;
    }
    for(int i=0;i<26;i++)vec[i].clear();
    for(int i=1;i<=l;i++)vec[s[i]-'a'].push_back(i);
    for(int i=0;i<26;i++){
        reverse(vec[i].begin(),vec[i].end());
    }
    for(int i=0;i<26;i++){
        scanf("%d%d",&L[i],&R[i]);
    }
    int ntag=0;
    for(int t=1;t<=k;t++){
        bool flag=0;
        for(int i=0;i<26;i++){
            while(vec[i].size()&&vec[i][vec[i].size()-1]<=ntag){
                vec[i].pop_back();
            }
            int ed=vec[i].size()-1;
            if(ed<0)continue;
            //cout<<check(1,0)<<endl;
            if(check(vec[i][ed],i,t)){
                ans[la++]=i+'a';//cout<<"get : "<<i<<" "<<endl;
                L[i]=max(0,L[i]-1);
                R[i]=max(0,R[i]-1);
                ntag=vec[i][ed];
                vec[i].pop_back();
                flag=1;
                break;
            }
        }
        if(!flag)break;
    }
    if(la!=k)puts("-1");
    else{
        ans[la]='\0';
        puts(ans);
    }
    return 0;
}
int main(){
    while(scanf("%s",s+1)!=EOF)w33ha();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值