They Are Everywhere

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 numbern - 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
  1. 3
    AaA
output
  1. 2
input
  1. 7
    bcAAcbc
output
  1. 3
input
  1. 6
    aaBCCe
output
  1. 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.

分析:尺取法;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #include <ext/rope>
  16. #define rep(i,m,n) for(i=m;i<=n;i++)
  17. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  18. #define vi vector<int>
  19. #define pii pair<int,int>
  20. #define mod 1000000007
  21. #define inf 0x3f3f3f3f
  22. #define pb push_back
  23. #define mp make_pair
  24. #define fi first
  25. #define se second
  26. #define ll long long
  27. #define pi acos(-1.0)
  28. const int maxn=1e5+;
  29. const int dis[][]={{,},{-,},{,-},{,}};
  30. using namespace std;
  31. using namespace __gnu_cxx;
  32. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  33. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  34. int n,m,mi,co[],cnt[],all;
  35. char a[maxn];
  36. int main()
  37. {
  38. int i,j,k,t;
  39. mi=inf;
  40. scanf("%d%s",&n,a);
  41. rep(i,,n-)if(++co[a[i]]==)all++;
  42. int l=-,r=,now=;
  43. for(i=;;i++)
  44. {
  45. if(++cnt[a[i]]==)now++;
  46. if(now==all)break;
  47. }
  48. r=i;
  49. mi=r+;
  50. while(l<=r&&r<n)
  51. {
  52. do{l++;if(--cnt[a[l]]>)mi=min(mi,r-l);else break;}while();
  53. do{r++;}while(r<n&&++cnt[a[r]]!=);
  54. if(r<n&&r>=l)mi=min(mi,r-l);
  55. }
  56. printf("%d\n",mi);
  57. //system ("pause");
  58. return ;
  59. }

随机推荐

  1. CRC的校验原理

    一.基本原理 CRC检验原理实际上就是在一个p位二进制数据序列之后附加一个r位二进制检验码(序列),从而构成一个总长为n=p+r位的二进制序列:附加在数据序列之后的这个检验码与数据序列的内容之间存在着 ...

  2. 关于malloc和free函数的用法

    原文:http://blog.pfan.cn/vfdff/33507.html 个人总结 在C语言的学习中,对内存管理这部分的知识掌 握尤其重要!之前对C中的malloc()和free()两个函数的了 ...

  3. 理解VMware虚拟机下网络连接的三种模式(如何配置虚拟机上网)

    很多朋友都用vmware来测试不同的系统,我结合自己的经验谈一下对网络设置的理解,不对的地方请指正. bridge:这种方式最简单,直接将虚拟网卡桥接到一个物理网卡上面,和linux下一个网卡 绑定两 ...

  4. mybatis 总结(1)

    注意事项 1.在使用type 和JavaType 以及reusltType ,ofType的时候一定要设置"别名"在mybatis.cfg.xml中设置 <typeAlias ...

  5. c#操作oracle的通用类

    using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...

  6. C#输出日历

    用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑. 1.控制台输出: using System; namespace 控制台日历 ...

  7. ActionBar更改背景颜色(主题)

    1.默认是黑色的背景, 2.更改主题theme为Theme.AppCompat.Light即可,清单文件主题如下: <application android:name="com.ith ...

  8. Linux 终端下颜色的输出

    在命令行下也能产生五颜六色的字体和图案,只需要加上一些颜色代码,例如 echo -e "\033[41;36m 红底绿字\033[0m" 其中41的位置代表底色, 36的位置是代表 ...

  9. “Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED”

    1. 有可能你的客户端已经安装过了,需要移调才能安装2. 你的清单文件AndroidManifest.xml写的有问题,检查一下

  10. 依赖注入(DI)和控制反转(IOC)【回顾】

    在java开发中广泛的使用了IOC的思想,在PHP中同样也在广泛使用. interface Coder { public function coding(); } 实现类Javaer class Ja ...