最短的包含字符串

来源:互联网 发布:下载头像的软件 编辑:程序博客网 时间:2024/05/23 05:09
给出一个字符串,求该字符串的一个子串S,S包含A-Z中的全部字母,并且S是所有符合条件的子串中最短的,输出S的长度。如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。
Input
第1行,1个字符串。字符串的长度 <= 100000。
Output
输出包含A-Z的最短子串长度。如果没有符合条件的子串,则输出No Solution。
Input示例
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output示例
28

#include <iostream>#include <string>#include <string.h>using namespace std;int nums[26];void fun(string &input){    int len = input.length();    memset(nums, 0, sizeof(nums));    int count = 0;    int left = 0;     int right = 0;    int result = len+1;    while (right < len)    {        char cur = input[right];                if (nums[cur-'A'] == 0)        {            nums[cur-'A'] = 1;            count++;        }        else        {            nums[cur-'A']++;        }                    if (count == 26)        {while (nums[input[left]-'A'] > 1){nums[input[left]-'A']--;left++;}            int temp = right - left + 1;            if (temp < result)            {                result = temp;            }        }                right++;    }        if (result < len+1)    {        cout << result << endl;        return;    }    cout << "No Solution" << endl;}int main(){    string input;    cin >> input;    fun(input);        return 0;}


0 0