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

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

发布了302 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/99415733