51nod 1127 最短的包含字符串(尺取法)

来源:互联网 发布:java怎么写游戏 编辑:程序博客网 时间:2024/06/06 07:02

题目:
这里写图片描述

简单尺取法

代码:

#include <iostream>#include <algorithm>#include <map>#include <vector>#include <set>#include <math.h>#include <queue>#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string>using namespace std;typedef long long ll;#define INF 2147483647string s; map <char,int> m;int main() {    cin >> s;    int len = s.length();    int l = 0,r = 0;int num = 0;    int ans = 2000000000;    while(true){        while(num < 26 && r < len){            if(m[s[r]] == 0) num++;            m[s[r]]++;            r++;        }        if(num < 26) break;        while(num == 26 && l < r){            if(m[s[l]] == 1) num--;            m[s[l]]--;            l++;        }        ans = min(ans,r-l+1);    }    if(ans == 2000000000) cout << "No Solution" << endl;    else cout << ans << endl;    return 0;}