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

Codeforces Round #364 (Div. 2) C

时间:2016-07-24 17:39:17      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

Description

Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won‘t let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

Input

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.

The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Output

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.

Examples
input
3
AaA
output
2
input
7
bcAAcbc
output
3
input
6
aaBCCe
output
5
Note

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.

题意:就是寻找最小的区间,它能包含该字符所有字母

我们设立起点和终点,l,r;

l从该字母第一次出现开始,如果后面该字母再次出现,就移动到另外一个字母第一次出现的位置

r从头遍历到尾,如果遍历符合条件就更新一次

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int flag[100005];
int main()
{
    set<char>q;
    int a[100005];
    string s;
    int n;
    int sum;
    int l=0,r=0;
    int len=(1<<30);
    cin>>n;
    cin>>s;
    for(int i=0;i<n;i++)
    {
        q.insert(s[i]);
    }
    sum=q.size();
    for(int r=0;r<n;)
    {
        int cot=s[r]-‘0‘;
        flag[cot]++;
        if(flag[cot]==1)
        {
            sum--;
        }
   //     cout<<flag[cot]<<"B"<<endl;
        //cout<<flag[s[l]
        while(flag[s[l]-‘0‘]>1)
        {
            flag[s[l]-‘0‘]--;
            l++;
          //  cout<<l<<"A"<<endl;
        }
        if(sum==0)
        {
        len=min(len,r-l+1);
     //   cout<<r<<endl;
      //  cout<<l<<endl;
        }
        r++;
     //   cout<<r<<"A"<<endl;
    }
    cout<<len<<endl;
    return 0;
}

  

Codeforces Round #364 (Div. 2) C

标签:

原文地址:http://www.cnblogs.com/yinghualuowu/p/5701136.html

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