码迷,mamicode.com
首页 > 其他好文 > 详细

51nod 1127 最短的包含字符串

时间:2018-10-24 01:20:01      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:pac   else   --   code   can   ext   har   output   NPU   

给出一个字符串,求该字符串的一个子串s,s包含A-Z中的全部字母,并且s是所有符合条件的子串中最短的,输出s的长度。如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。
 
Input
第1行,1个字符串。字符串的长度 <= 100000。
Output
输出包含A-Z的最短子串s的长度。如果没有符合条件的子串,则输出No Solution。
Input示例
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output示例
28

尺取。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAX 100001
using 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 最短的包含字符串

标签:pac   else   --   code   can   ext   har   output   NPU   

原文地址:https://www.cnblogs.com/8023spz/p/9840305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!