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
    
    
  • 相关阅读:
    病毒木马查杀实战第017篇:U盘病毒之专杀工具的编写
    病毒木马查杀实战第016篇:U盘病毒之逆向分析
    病毒木马查杀实战第015篇:U盘病毒之脱壳研究
    病毒木马查杀实战第014篇:U盘病毒之手动查杀
    病毒木马查杀实战第024篇:MBR病毒之编程解析引导区
    病毒木马查杀实战第023篇:MBR病毒之引导区的解析
    缓冲区溢出分析第11课:整数溢出的原理
    缓冲区溢出分析第10课:Winamp缓冲区溢出研究
    Backdoor.Zegost木马病毒分析(一)
    缓冲区溢出分析第09课:MS06-040漏洞研究——深入挖掘
  • 原文地址:https://www.cnblogs.com/lglh/p/11250140.html
Copyright © 2011-2022 走看看