HDU 6586 String 2019HDU多校赛第一场1009【贪心】


传送门:HDU 6586


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



题意:
给你一个串,现需要你给出一个子序列,满足26个约束条件,len(ai)>=Li 且 len(a)<=Ri, ai为从a到z的26个字母。
约束条件为每次字母的出现次数的限制。


题解:
(官方)一位位地构造答案字符串,每次贪心地加能加入的最小的字符 (判断能否加入只要判断 加入之后原字符串剩下的后缀中的每种字符的数目能否足够满足条件)。

每次贪心地加入最小的字符,加入的条件为当前字母加入后,后面的字符满足剩余的条件。
(剩余的字母ai+已拿取字母ai>=Li且满足Li所需的长度小于剩余可添加长度。)



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
const int N = 1e5 + 7;
char s[N];
int k;
int sum[N][30], l[30], r[30], used[30];
int nex[N][30];
char ans[N];
bool check(int pos,int nowlen)//判断该为位置是否符合
{
  int len=0,last=0;//len:记录能到达的最大长度,last:记录还没满足最小限制的字母数
  for(int i=0;i<26;i++)
  {
    if(used[i]+sum[pos][i]<l[i]) return false;//如果该字母已经用过的数目加上当前位置后还剩的该字母的数目仍然小于最小限制
    len+=used[i]+min(sum[pos][i],r[i]-used[i]);
    last+=max(0,l[i]-used[i]);
  }
  if(len<k) return false;//选了该点后能到达的最大长度仍然小于k
  if(last>k-nowlen) return false;//选了该点后,没满足最小限制的字母数比能构成字符串的剩余长度大
  return true;
}
int main()
{
	while(~scanf("%s%d",s,&k))
  {
    memset(used,0,sizeof(used));//记录每个字符已经用了多少
    memset(sum,0,sizeof(sum));//记录后缀字符的数目
    memset(nex,-1,sizeof(nex));//记录i位置的后缀字符j第一个位置
    for(int i=0;i<26;i++)
    {
      scanf("%d%d",&l[i],&r[i]);
    }
    int len = strlen(s);
    for(int i=len-2;i>=0;i--)//计算后缀字符
    {
      for(int j=0;j<26;j++)
      {
        sum[i][j]=sum[i+1][j];
        nex[i][j]=nex[i+1][j];
      }
      sum[i][s[i+1]-'a']++;
      nex[i][s[i+1]-'a']=i+1;
    }
    int pos=0,tot=0;//pos:当前位置,tot:已经构造的长度
    bool isok=true;//判断是否可以构造
    while(pos<len&&tot<k)//构造
    {
      bool flag=false;
      for(int i=0;i<26;i++)
      {
        if(nex[pos][i]!=-1&&used[i]<r[i])
        {
          used[i]++;
          if(check(nex[pos][i],tot+1))
          {
            ans[tot++]=i+'a';//满足条件放入
            pos=nex[pos][i];//更新位置
            flag=true;
            break;
          }
          used[i]--;//上述判断不满足,则该字母没有使用
        }
      }
      if(!flag)//没有一个字符满足条件,可以放置
      {
        isok=false;
        break;
      }
    }
    ans[tot]='\0';
    if(isok) printf("%s\n",ans);
    else printf("-1\n");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值