蓝桥杯刷题记录

Problem F. Wiki with String
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
现在有一个字符串ss中只包含数字字符0-9,现在需要输出一个子字符串k满足以下条件:
条件1k包含0-9中所有的数字字符;
条件2:在所有符合要求的子字符串中, k的长度最小;
条件3:如果存在多个满足条件1和条件2的子字符串,请输出字典序最小的那个子字符串。
Input
输入一个字符串s,且s的长度不大于106
Output
输出符合要求的子字符串k;如果不存在符合要求的子字符串,请输出-1
Samples

standard input standard output
00123489765 0123489765
1234567890123456789 0123456789
123456789123 -1


思路:

利用双指针找到符合要求的,存在数组里,输出符合条件的字符串

code:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std ;

int st[10] ;

bool check(){
    for(int i=0;i<10;i++){
        if(!st[i]){
            return false ;
        }
    }
    return true ;
}

bool cmp(string a,string b){
    if(a.size() != b.size()){
        return a.size() < b.size() ;
    }else{
        return a < b ;
    }
}

int main(){
    string str1 ;
    cin >> str1 ;
    vector<string> vc ;
    int ls = str1.size() ;
    int i=0,j=0 ;
    while(j<ls){
        st[str1[j]-'0'] ++ ;
        while(check()){
            vc.push_back(str1.substr(i,j-i+1)) ;
            st[str1[i]-'0'] -- ;
            i++ ;
        }
        j++ ;
    }
    if(vc.size()>0){
        sort(vc.begin(),vc.end(),cmp) ;
        cout << vc.front() << endl ;
    }else{
        cout << "-1" << endl ;
    }
    
    
    return 0 ;
}
 
原文地址:https://www.cnblogs.com/gulangyuzzz/p/12016917.html