zoukankan      html  css  js  c++  java
  • 杭电2019多校第一场,Problem I,String 2019

    题目描述

    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.
     

    输入

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

    样例输入

    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
    

    样例输出

    abb
    题解:
    将字母下标按字母从前往后依次抽取出来;
    然后从前往后依次确定k的每一位应该放的字母;
    对于每一位,枚举字母的顺序应该是从a到z,接着判断该字母放上去后是否符合条件,符合则去确定k的下一位字母,不符合则继续循环;
    时间复杂度应该是O(26*26*n)。
    AC代码:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=1e5+50;
     4 int suf_sum[maxn][30],l[30],r[30],used[30],n,k,last;
     5 char str[maxn],res[maxn];
     6 vector<int> letter_index[30];
     7 int main()
     8 {
     9     while(scanf("%s %d",str,&k)!=EOF)
    10     {
    11         for(int i=0;i<=25;i++){
    12             scanf("%d %d",&l[i],&r[i]);
    13             letter_index[i].clear();
    14         }
    15         vector<int> ::iterator head[30];
    16         memset(used,0,sizeof(used));
    17         n=strlen(str);last=-1;
    18         for(int i=0;i<=n;i++) for(int j=0;j<=25;j++) suf_sum[i][j]=0;
    19         for(int i=n-1;i>=0;i--){
    20             for(int j=0;j<=25;j++){
    21                 if(str[i]=='a'+j) suf_sum[i][j]=suf_sum[i+1][j]+1;
    22                 else suf_sum[i][j]=suf_sum[i+1][j];
    23             }
    24         }
    25         for(int i=0;i<=n-1;i++){
    26             letter_index[str[i]-'a'].push_back(i);
    27         }
    28         for(int i=0;i<=25;i++){
    29             head[i]=letter_index[i].begin();
    30         }
    31         bool ans=true;
    32         for(int i=0;i<=k-1;i++){
    33             bool flag=false;
    34             for(int j=0;j<=25;j++){
    35                 if(used[j]==r[j]) continue;
    36                 while(head[j]!=letter_index[j].end() && (*head[j])<=last) head[j]++;
    37                 if(head[j]==letter_index[j].end()) continue;
    38                 used[j]++;
    39                 bool tag=true;
    40                 int cnt=0,tmp=0,pos=(*head[j]);
    41                 for(int t=0;t<=25;t++){
    42                     if(suf_sum[pos+1][t]+used[t]<l[t]) tag=false;
    43                     cnt+=max(0,l[t]-used[t]);
    44                     tmp+=min(suf_sum[pos+1][t],r[t]-used[t]);
    45                 }
    46                 if(cnt>k-1-i || tmp<k-1-i) tag=false;
    47                 if(!tag) used[j]--;
    48                 else{
    49                     res[i]='a'+j;
    50                     last=pos;
    51                     flag=true;
    52                     break;
    53                 }
    54             }
    55             if(!flag){
    56                 ans=false;
    57                 printf("-1
    ");
    58                 break;
    59             }
    60         }
    61         if(ans){
    62             res[k]='';
    63             printf("%s
    ",res);
    64         }
    65     }
    66     return 0;
    67 }
    68 /*
    69 aaccddaa 6
    70 2 4
    71 0 0
    72 2 2
    73 0 2
    74 0 0
    75 0 0
    76 0 0
    77 0 0
    78 0 0
    79 0 0
    80 0 0
    81 0 0
    82 0 0
    83 0 0
    84 0 0
    85 0 0
    86 0 0
    87 0 0
    88 0 0
    89 0 0
    90 0 0
    91 0 0
    92 0 0
    93 0 0
    94 0 0
    95 0 0
    96 */
    View Code
    
    
  • 相关阅读:
    堆的创建、优先队列、topk、堆排序C语言实现
    HTTPS加密原理
    go shard map实现
    Python进程间通信
    TCP 半连接队列和全连接队列
    WireShark过滤语法
    TCP拥塞机制
    【企业管理实务系列】低值易耗品管理办法
    CV之Face Change:基于人工智能实现国内众多一线美女明星换脸(基于Face++输出4*106个特征点定位+融合代码、deepfake技术)
    【转发】农行银企直联XML对接socket SAP EPIC
  • 原文地址:https://www.cnblogs.com/lglh/p/11250140.html
Copyright © 2011-2022 走看看