51nod 1127 contains the shortest string

Source: Internet
Author: User
Returns the length of a substring S, which contains all the letters in the A-Z, and s is the shortest length of output s for all qualified substrings. If the given string does not include all the letters in the A-Z, no solution is output. Input
1st rows, 1 string. The length of the string <= 100000.
Output
Output the length of the shortest substring s containing the A-Z. If no sub-string meets the condition, no solution is output.
Input example
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output example
28

Ruler.
Code:
#include <iostream>#include <cstdio>#include <algorithm>#define MAX 100001using namespace std;char s[MAX];int m = MAX;int main() {    int head = 0,tail = 0,c = 0,num[26] = {0};    scanf("%s",s);    while(s[tail] || c == 26) {        if(c < 26) {            int d = s[tail ++] - ‘A‘;            if(!num[d]) c ++;            num[d] ++;        }        else {            m = min(m,tail - head);            int d = s[head ++] - ‘A‘;            if(num[d] == 1) c --;            num[d] --;        }    }    if(m != MAX)printf("%d\n",m);    else printf("No Solution\n");}

 

51nod 1127 contains the shortest string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.