zoukankan      html  css  js  c++  java
  • 51nod 1127 最短的包含字符串

    给出一个字符串,求该字符串的一个子串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
    ",m);
        else printf("No Solution
    ");
    }
  • 相关阅读:
    装饰器
    初始面向对象
    生成器迭代器
    初识函数
    文件操作
    数据类型补充

    集合 元祖 字典
    Python练习题 034:Project Euler 006:和平方与平方和之差
    Python练习题 033:Project Euler 005:最小公倍数
  • 原文地址:https://www.cnblogs.com/8023spz/p/9840305.html
Copyright © 2011-2022 走看看